113👍
✅
The code is wrong because Person
will get a reverse relationship back to Person2Person.person
, and also to Person2Person.friend
; the default name would be Person.person2person_set
but Django can’t use the same name for both.
So you could set a related_name
on either, or both:
class Person2Person(models.Model):
person = models.ForeignKey(Person, related_name='person2persons')
friend = models.ForeignKey(Person, related_name='friends')
Now Person.friend
‘s related to the Person2Person
objects that have this Person
as a friend, and Person.person2person
to the ones that have this Person as a person.
However, why aren’t you using a ManyToManyField
to 'self'
on Person
?
Source:stackexchange.com