Wednesday, September 3, 2014

Django custom RelatedManager with ManyToManyField for multi-record inheritance

I have the following object structure:
class Car(models.Model):
    name
= models.CharField(max_length=16)

class Person(models.Model):
    name
= models.CharField(max_length=32)
    cars
= models.ManyToManyField(Car, through='Relationship')
    parent
= models.ForeignKey('self', null=True, blank=True, related_name='childrens')

class Relationship(models.Model):
    car
= models.ForeignKey(Car)
    person
= models.ForeignKey(Person)
    love
= models.BooleanField(_('love status'), default=True, db_index=True)

The logic is of the models is:
  • Person may be have another Person (children, for instance-ForeignKey on 'self').
  • cars (related manager) should return a cars of the current related person + a cars, that his father (if exists) have (inheritance).
  • children can't add to self a car, that his father already have (inheritance).
  • relationship_set manager should work by the same logic of cars manager (if a father loves a car, then a children should love it too.
This logic looks like inheritance. I can't implement this logic by Multi-table inheritance, because achildren may have other children. Maybe there is some ideas like multi-record inheritance?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/90efde45-fdd0-4061-9d3e-bd374b5b29c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment