I want to implement a basic social network model in django, i.e., followers and followees
class CustomUser(User):
followers = models.ManyToManyField('self', related_name='followees',
related_query_name='followee')
and above is what I define.I think this will be enough because the manytomany field provide backtrack query. Do I still need to create a followees ManyToMany filed?
And could anybody help write the function how to get the number of followers and followees, as well as followers list and followees list. I'm new to django So I'm confused about the many-to-many field
@olieidel is right, but here is one more REALLY important thing, you shouldn't miss in your example: normally, M2M-relations are symmetrical. It means, that if you set User A as follower of User B, querysets will also return User B as follower of User A.
You can avoid such behavior by adding symmetrical=False
option to the field:
followers = models.ManyToManyField(
to='self',
related_name='followees',
symmetrical=False
)
No, you don't have to create a (second) followees ManyToMany field. This is already taken care of by specifying related_name='followees'
. On a sidenote, there's no need to specify related_query_name
as its value is set to the value of related_name
by default.
A ManyToManyField
returns a RelatedManager
which allows you to do some queries on it similar to a QuerySet. In your case you're interested in the count()
:
custom_user = CustomUser().save()
# add followers etc..
followers_count = custom_user.followers.count()
followees_count = custom_user.followees.count()
The Django Docs have some nice examples for ManyToMany Relationships.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With