I’m trying to accomplish something akin to twitter on my website, where one user can follow another one. I want to select all User records that are following a User with a given ID. My follow relationship model look like:
class Following(models.Model):
user = models.ForeignKey(User, related_name='is_following')
followed = models.ForeignKey(User, related_name='is_followed')
And I’m using the django.contrib.auth.models User class.
Assuming I have a User object named “myUser” representing the followed person, what’s the appropriate query to get everyone following them?
mipadi’s answer is missing ‘
.all‘.In a view, the queryset
returns a list of
Followingobjects where my_user is being followed. To get the user that is following from aFollowingobjectf, usef.userIf followers is in the template context, you could do the following.
You might find the Django documentation for many to many relationships useful.