1👍
✅
Error creating several recursive m2m relationships seems like a related question. I realized that I could get the same effect by removing the parents
, children
, spouse
fields from the Person
model and setting the related_name to relationships
on the Relationship
model.
class Person(models.Model):
[ other fields ]
class Relationship(models.Model):
personA = models.ForeignKey(Person, related_name='relationships')
personB = models.ForeignKey(Person, blank=True, null=True)
person_name = models.TextField(default='', blank=True)
relationship = models.CharField(choices=(('s', 'spouse'),
('c', 'child'),
('p', 'parent')),
default='s')
I haven’t tested it yet, but it does validate and doesn’t throw the error messages any more. This also seems cleaner.
I imagine I could create some properties on the Person
model to give me access to say Person.spouse
which returns all Person.relationships
where the relationship == 's'
.
Source:stackexchange.com