django-models

How can I use django forms/models to represent choices between fields?

How can I use boolean choices in a model field to enable/disable other fields. If a boolean value is true/false I want it to enable/disable other model fields. Is there a way to natively express these relationships using django models/forms/widgets? I keep writing custom templates to model these relationships, but can't figure out a g...

How do I use django mptt?

I have a model: class Company(models.Model): name = models.CharField( max_length=100) parent = models.ForeignKey('self', null=True, blank=True, related_name='children') mptt.register(Company, order_insertion_by=['name']) and class Financials(models.Model): year = models.IntegerField() revenue = models.DecimalField(ma...

How do I implement a common interface for Django related object sets?

Here's the deal: I got two db models, let's say ShoppingCart and Order. Following the DRY principle I'd like to extract some common props/methods into a shared interface ItemContainer. Everything went fine till I came across the _flush() method which mainly performs a delete on a related object set. class Order(models.Model, interface...

Django: retrieving abstract-derived models

After getting fine answer to my previous question, I came across another problem. I followed the third approach, being aware of what djangodocs say about abstract model subclassing. I am using the latest Django, rev 9814. The strange behaviour I get: In [1]: o = Order() In [2]: o.save() DEBUG:root:STORING EVENT MESSAGE: Order created...

BinaryFields in Django Models

I have a twenty byte hex hash that I would like to store in a django model. If I use a text field, it's interpreted as unicode and it comes back garbled. Currently I'm encoding it and decoding it, which really clutters up the code, because I have to be able to filter by it. def get_changeset(self): return bin(self._changeset) de...

How to work with unsaved many-to-many relations in django?

I have a couple of models in django which are connected many-to-many. I want to create instances of these models in memory, present them to the user (via custom method-calls inside the view-templates) and if the user is satisfied, save them to the database. However, if I try to do anything on the model-instances (call rendering methods,...

Is there a way to auto-increment a Django field with respect to a foreign key?

I'm currently coding a site in Django (because you can never learn too many frameworks!), and am wondering if there's a way to auto-increment an object with respect to a related object. So for instance, say I have an Entry model and an EntryVersion model: Entry - title - slug EntryVersion - entry (foreign key) - version_number - conte...

How to externally populate a Django model?

What is the best idea to fill up data into a Django model from an external source? E.g. I have a model Run, and runs data in an XML file, which changes weekly. Should I create a view and call that view URL from a curl cronjob (with the advantage that that data can be read anytime, not only when the cronjob runs), or create a python scr...

How would you populate a field based on another field

From the admin panel I want to populate a slug field based on a certain text field eg. Title: My Awesome Page would automaticaly populate Slug: my_awesome_page ...

Django: Perform case-insensitive lookups by default

I need to perform case-insensitive queries on username by default when using the Django Auth framework. I tried fixing the issue by writing a custom subclass of Queryset and overriding the _filter_or_exclude method and then using that subclass in a custom manager for the User model- from django.db.models import Manager from django.db.m...

Can I have a Django model that has a foreign key reference to itself?

Okay, how would I do this? class Example(models.Model): parent_example = models.ForeignKey(Example) I want to have a model have a foreign key reference to itself. When I try to create this I get a django validation error that Example is not yet defined. ...

Display multiple related Django models in a table design problem

Hi there, I'm struggling with the design of a django application. Given the following models: class A(models.Model): name = models.CharField(max_length=255) class B(models.Model): name = models.CharField(max_length=255) a = models.ForeignKey(A) class C(models.Model): val = models.IntegerField() b = models.ForeignKe...

Multiple images per Model

I'm writing a simple real-estate listing app in Django. Each property needs to have a variable number of images. Images need to have an editable order. And I need to make the admin user-proof. So that said, what are my options? Is there a ImageList field that I don't know about? Is there an app like django.contrib.comments that does ...

django - database design - mutiple tables require 1+ phone_number(s)

Question regarding how to setup dbase relationships (newbie, this may be trivial) Followed the django tutorial (Poll, Choices); understood that 1 Poll has many Choice(s), therefore many Choice(s) point to a single Poll. class Poll(models.Model): question = models.CharField(max_length=200) ... class Choice(models.Mode...

How can I have two foreign keys to the same model in Django?

I want to have two foreign keys to the same model: class Test(models.model): example1 = models.ForeignKey(Example) example2 = models.ForeignKey(Example) I get errors like: Accessor for field 'example1' clashes with related field 'Example.test_set'. Add a related_name argument to the definition for 'example1'. ...

Django Model API reverse lookup of many to many relationship through intermediary table

I have a Resident and can not seem to get the set of SSA's the resident belongs to. I've tried res.ssa_set.all() .ssas_set.all() and .ssa_resident_set.all(). Can't seem to manage it. What's the syntax for a reverse m2m lookup through another table? EDIT: I'm getting an 'QuerySet as no attribute' error. Erm? class SSA(models.Model): ...

Django manager for _set in model

Hello, I'm in the progress of learning Django at the moment but I can't figure out how to solve this problem on my own. I'm reading the book Developers Library - Python Web Development With Django and in one chapter you build a simple CMS system with two models (Story and Category), some generic and custom views together with templates f...

Django Project structure, recommended structure to share an extended auth "User" model across apps?

I'm wondering what the common project/application structure is when the user model extended/sub-classed and this Resulting User model is shared and used across multiple apps. I'd like to reference the same user model in multiple apps. I haven't built the login interface yet, so I'm not sure how it should fit together. The following c...

Model next available primary key

How can i know the next free primary key of some model? ...

In Django, can you not use a string join on a ManyToManyField? Is ManyToMany not just a list?

I have two models in my Django project: Match Player Match has a ManyToMany property pointing at players, so that multiple players can compete in a match. I'd like to return an informative object name in the Django admin, something like "Richard Henry vs John Doe", by using a join on the players' full names. However the following fai...