Document presumed order of foreign keys on intermediate M2M model
|Reported by:||flytwokites@…||Owned by:||Oxylo|
|Has patch:||no||Needs documentation:||yes|
|Needs tests:||no||Patch needs improvement:||no|
When defining a many-to-many relationship from a model to itself and using an intermediary model,
class User(models.Model): name = models.CharField(max_length=100) followers = models.ManyToManyField('self', through='Relationship', symmetrical=False) def __unicode__(self): return self.name class Relationship(models.Model): target = models.ForeignKey('User', related_name='r1') follower = models.ForeignKey('User', related_name='r2') created_at = models.DateTimeField(auto_now_add=True)
It seems that django determine 'from field' and 'to field' by it's definition order,
the first field always used as 'from field' and the second field always used as 'to field'.
So I MUST put target field defintion above the follower field.
I checked the document but can not found any infomation to confirm it,
I think the document should clearly explained the rule.
Change History (3)
comment:1 Changed 4 years ago by gabrielhurley
- Needs documentation set
- Needs tests unset
- Patch needs improvement unset
- Summary changed from Need more document for defining a many-to-many relationship from a model to itself and using an intermediary model to Document presumed order of foreign keys on intermediate M2M model
- Triage Stage changed from Unreviewed to Accepted
- Type changed from Uncategorized to Cleanup/optimization
comment:2 Changed 11 months ago by Oxylo
- Owner changed from nobody to Oxylo
- Status changed from new to assigned