问题
I have the following - simplified - models:
class User(models.Model):
following = models.ManyToManyField("self", through='Following', symmetrical=False)
class Following(models.Model):
from_user = models.ForeignKey(User, related_name='from_user')
to_user = models.ForeignKey(User, related_name='to_user')
status = models.IntegerField()
The status is 0 for pending, 1 for following Let user be a User. I would like to get all the followed users of user
I can do
user.following.all()
to get all the users user is following (pending relationships OR really follow)
or
Following.objects.filter(from_user=user, status=1)
to get all the Following objects with User user and real friendship
But how can I get all the User objects for user and status=1 ? I can't seem to find a way
Thank you !
回答1:
Try
user.following.filter(to_user__status=1)
the user.following is still querying on User, thus you need to span relation w/ __ to Follow here.
The two fields here, from_user and to_user, are both ForeignKey pointing to User model. Thus for an User() instance u:
u.followingsearches for theUser()s who have relationship w/uthrough the intermediate table, theFollow. The key here is thatu.followingpicks the firstForeignKeyin theFollowthat points to theUser, as the reference touitself. Thus for your version ofFollow,u.following.filter(to_user__status=1)filters on theFollowitems havingfrom_userequals touandto_userw/statusequals to1. The lookup is typical following relationship backwardsu.from_usersearches the intermediate table for those havingfrom_userequals touu.to_usersearches the intermediate table for those havingto_userequals tou
Also, you could filter on the ForeignKey directly, w/ remembering that the from_user and to_user are both ref the Follow:
User.objects.filter(to_user__from_user=user, to_user__status=1) # user as from_user
User.objects.filter(from_user__to_user=user, from_user__status=1) # user as to_user
User.objects.filter(following=user) # those who are followed by `to_user` user
回答2:
Seems that
user.following.filter(to_user__status=1)
does the trick. Can someone explain to me why, and how this works?
user.following is a ManyRelatedManager which is querying on User. What would user.following.filter(from_user__status=1) do? Can't to guess what it returns...
Thanks again !
来源:https://stackoverflow.com/questions/13293068/django-asymettrical-self-through-relationship-query