Django custom RelatedManager with ManyToManyField for multi-record inheritance

95 views
Skip to first unread message

Dr. Sultan Alotaibi

unread,
Sep 3, 2014, 2:23:17 PM9/3/14
to django...@googlegroups.com
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?
Reply all
Reply to author
Forward
0 new messages