Tuesday, November 26, 2013

Other side of a ManyToManyField through self field

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,

--
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/41e1d864-4bec-459c-8a49-b3595253d463%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment