Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model inheritance, filtering models

Given the following models:(don't mind the TextFields there're just for illustration)

class Base(models.Model):
   field1 = models.TextField()

   class Meta:
      abstract=True

class Child1(Base):
   child1_field = models.TextField()

class Child2(Base):
   child2_field = models.TextField()


class Content(models.Model):
    aso_items = models.ManyToManyField('Base')

According to these definitions a Content object can be associated with more than one Base object, eg. an interview(=Content object) can be linked with a musician(=Child1 object), a filmdirector(=Child2), etc.

Now, for my question: Is it possible to filter Content objects according to which model the aso_items field points to? An example : Say I would like a Queryset containing all the Content objects that are associated with a specific object of Child1(eg. all the interviews associated with the musician Bob Dylan), how can I achieve this?

Further, what if I'd want a QuerySet containing all the Content objects that are associated with Child1 objects?(eg. all the interviews that associated with musicians) How does this change the filtering?

Thanks in advance ps: I'm experiencing some problems with white space in the preview, forgive me

like image 614
fvdnabee Avatar asked Dec 30 '22 15:12

fvdnabee


1 Answers

You should check the section of the Django docs regarding using related_name for abstract base classes. http://docs.djangoproject.com/en/dev/topics/db/models/#be-careful-with-related-name

To quote the docs:

If you are using the related_name attribute on a ForeignKey or ManyToManyField, you must always specify a unique reverse name for the field. This would normally cause a problem in abstract base classes, since the fields on this class are included into each of the child classes, with exactly the same values for the attributes (including related_name) each time.

To work around this problem, when you are using related_name in an abstract base class (only), part of the name should be the string %(class)s. This is replaced by the lower-cased name of the child class that the field is used in. Since each class has a different name, each related name will end up being different.

Using this information I would recommend moving the m2m field into the Base class:

class Content(models.Model):
   # Add remaining fields for Content 
   pass

class Base(models.Model):
   field1 = models.TextField()
   items = models.ManyToManyField(Content,related_name="%(class)s_related")

   class Meta:
      abstract=True

class Child1(Base):
   child1_field = models.TextField()

class Child2(Base):
   child2_field = models.TextField()
like image 112
Mark Lavin Avatar answered Jan 04 '23 14:01

Mark Lavin