Other side of a ManyToManyField through self field

13 views
Skip to first unread message

Jean-Baptiste Pressac

unread,
Nov 26, 2013, 5:38:27 AM11/26/13
to django...@googlegroups.com
Hello,
I have a Person model with a ManyToManyField with itself through a Relation model to declare relations like Paul is Julie's uncle :

class Relations(models.Model): 
    id_person= models.ForeignKey('Person', related_name='relation_id_person')
    id_relation = models.ForeignKey('Person', related_name='relation_id_relation')
    relation_type = models.ForeignKey('RelationType')                                      # lists of relation types, as uncle, niece, father, son ...
    day_starts_relation = models.CharField(max_length=50L, blank=True)
    month_starts_relation = models.CharField(max_length=50L, blank=True)
    year_starts_relation = models.CharField(max_length=50L, blank=True)
    day_ends_relation = models.CharField(max_length=50L, blank=True)
    month_ends_relation = models.CharField(max_length=50L, blank=True)
    year_ends_relation = models.CharField(max_length=50L, blank=True)
    notes = models.TextField(blank=True)
    def __unicode__(self):
        return u"{0} {1}".format(self.id_person,self.id_relation)

class Person(models.Model):
    name = models.CharField(max_length=255L)
    relations = models.ManyToManyField("self", through='Relations', related_name='person.relations', symmetrical = False)

I used the Django admin to create and display the Person model, however, the "relations" field is not displayed on the "other side" of the relation. i.e. if I create a person named "Paul" uncle of "Julie", the fact that Paul is Julie's uncle won't be displayed on Julie's form.

I suppose I have two solutions :
1. To add the possibility to create the relation that Julie is Paul's niece on Julie's form, but this relation should share the dates (day_starts_relation, month_starts_relation ...) with the relation "Paul uncle of Julie"
2. To display as read only on Julie's form the relation "Paul uncle of Julie"

But how could I modify Django's normal behavior to enable those solutions ? What is the best solution in your point of view ?

Thanks,
Reply all
Reply to author
Forward
0 new messages