[Answer]-Model friends table in Django

1👍

The approach with the ManyToMany field looks good enough. If you were to create another model to hold this manually you would need to add logic to avoid duplication (maybe other things too).

Also, with the ManyToMany you end up with users that have contacts…you can for example do this:

my_user.person.contacts.all()
my_user.person.contacts.add(another_user)
my_user.person.contacts.filter(phone='123456')

With the other approach you would need to run queries from Contact model:

Contact.objects.filter(user_1=pk1, user_2=pk2)
Contact.objects.create(user_1=pk1, user_2=pk2) # add logic or db constraints to prevent duplication

It is not that complicated, but the first one does make more sense for this case.

Leave a comment