django-models

Custom QuerySet and Manager without breaking DRY?

I'm trying to find a way to implement both a custom QuerySet and a custom Manager without breaking DRY. This is what I have so far: class MyInquiryManager(models.Manager): def for_user(self, user): return self.get_query_set().filter( Q(assigned_to_user=user) | Q(assigned_to_group__in=...

Django: overriding to_python() on a ForeignKey field and what other methods are called before clean_<fieldname> when validating a form?

I'm trying to solve a problem that I outlined in this question. At the moment it looks like I will have to override the to_python() method on the ForeignKey field. But as far as I can see in django's source code, the ForeignKey class doesn't actually have a to_python() method declared, so it must be inheriting it from the Field class, wh...

Django: Queryset.clear() method on OneToOneField()

I need to avoid cascading deletes on a foreign key, but it's a OneToOneField(), like: class MyModel(models.Model): def delete(self): self.mysubmodel.clear() # Breaks because self.cartitem is not a QuerySet. super(MyModel, self).delete() class MySubModel(models.Model): mymodel = models.OneToOneField(MyModel) T...

Do inline model forms emmit post_save signals? (django)

So I have two models (Tables) related by a ForeignKey. In the admin, the edit page displays the first model (let's say ModelOne) along with the related instances of the second model, ModelTwo (TabularInline). What I want is perform some additional actions when the second model is being changed. I can do this with a post_save signal on M...

Custom address field in Django Model

What's the common practice to represent postal addresses in Django models? Is there a library for custom model fields that include postal address fields and potentially handle validation and formatting? If no library exists, how can I write one? Can I represent a composite field (a field that gets serialized to multiple columns in db)...

Django and PHP together

def hi_guys(): question=" I have a django application and a php script. I want that php script interacts with the tables used by django ( with INSERT and DELETE ). Are there problems with django ? Thanks <:-)" print question ...

QuerySet for non-empty TextField

For a model like: class Item(models.Model): notes = models.TextField(blank=True) .... I'm attempting to do a simple queryset for all Items where the "notes" field is non-empty. Not finding mention of this capability in the docs, but via a comment on a bug report, discovered that you can actually compare with greater than: ite...

How to create a unique_for_field slug in Django?

Django has a unique_for_date property you can set when adding a SlugField to your model. This causes the slug to be unique only for the Date of the field you specify: class Example(models.Model): title = models.CharField() slug = models.SlugField(unique_for_date='publish') publish = models.DateTimeField() What would be the...

Django ORM: SELECT on field attributes

Hello everyone, I have a model that contains a FileField. I want to search for a specific filename. How do I do it? I was trying: MyModel.objects.get(document__name=FOO) I got a Join on field 'document' is not permitted. Thanks! ...

Django-models ForeignKey Objects display Field Name instead of Object Values

I am using a ForeignKey called Memberno in My Django models. After entering data in the Members Class, it appears in the other classes as "member object" in the 'memberno' field. For ForeignKey, I am using 'raw_id_fields' as a workaround, but for normal fields and even 'filter_horizontal' , each memberno value is displayed as "member obj...

Using python properties in django models?

My problem relates to this question: http://stackoverflow.com/questions/1390556/default-ordering-for-m2m-items-by-intermediate-model-field-in-django class Group(models.Model): name = models.CharField(max_length=128) _members = models.ManyToManyField(Person, through='Membership') @property def members(self): return sel...

How do I extend the Django Group model?

Is there a way to extend the built-in Django Group object to add additional attributes similar to the way you can extend a user object? With a user object, you can do the following: class UserProfile(models.Model): user = models.OneToOneField(User) and add the following to the settings.py file AUTH_PROFILE_MODULE = 'app.UserProf...

Distinct elements in django model

I'm a Django newbie and I wonder if there is a more efficient way (at a database level) of doing the following. I have the model: class Foo(models.Model): item=models.IntegerField() another_item=models.IntegerField() And want to get an iterable of all the distinct values of "item". This is what I have so far: distinct=set([...

Where is meta.local_fields set in django.db.models.base.py ?

I'm getting the error: Exception Value: (1110, "Column 'about' specified twice") As I was reviewing the Django error page, I noticed that the customizations the model User, seem to be appended to the List twice. This seems to be happening here in django/db/model/base.py in base_save(): values = [(f, f.get_db_prep_save(raw and get...

Model won't validate in Django even with related_name set correctly

I have two abstract models: class Createable(models.Model): createLog = models.ForeignKey(ActionLog, related_name="%(app_label)s_%(class)s_create_action_log") class Meta: abstract = True class Deleteable(models.Model): deleteLog = models.ForeignKey(ActionLog, related_name="%(app_label)s_%(class)s _delete_action_...

Django ORM's "related data" loading behavior

In LINQ (I come from a C# background), you can manually load data for related tables via the Include("xxx") method from a in ctx.MainTable.Include("SubTable") select a; In the above code, every instance of MainTable is loaded and all the data for MainTable.SubTable is also loaded. If "Include" is not called, every returned MainTable'...

how to use array in django

Hi , I have a db table which has a integer array.But how can i add this field in my model .I tried writing it using IntegerField but on save it is giving error int() argument must be a string or a number, not 'list how can i add this field to my model.I m using this field in my views.py so i need to add it inmy model.Any suggestions...

Django: Using custom primary key, should I specifiy unique=True?

I read this page: http://www.djangoproject.com/documentation/models/custom_pk/, and the example doesn't list unique=True. I'm wondering if there is a compelling reason for them to leave it out, or if there is some reason I should include it. My assumption is that specifying primary_key=True does this automatically though. Any thoughts? ...

Django: how to define two fields "unique" as couple

Is there a way to define a couple of fields as unique in Django? I have a table of volumes (of journals) and I don't want more then one volume number for the same journal. class Volume(models.Model): id = models.AutoField(primary_key=True) journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Jour...

Using a method in a model to count objects filtered by the primary key.

I wanted to use a method as part of my model to count all the occurrences of the object in another table that references it as a foreign key. Will the below work? class Tile(models.Model): #... def popularity(self): return PlaylistItem.objects.filter(tile__exact=self.id).count() And the relevant information from the p...