i have a models
class FriendsWith(models.Model):
username = models.ForeignKey(User,on_delete=models.CASCADE)
fusername =models.ForeignKey(User,on_delete=models.CASCADE,related_name='fusername')
time = models.DateTimeField(auto_now_add=True)
confirm_request = models.SmallIntegerField(default=1)
blocked_status = models.IntegerField(default=0)
i wanted to search all the friends of currently logged in user.So,i am doing like this
obj1=FriendsWith.objects.filter(username=request.user).select_related('fusername')
obj2=FriendsWith.objects.filter(fusername=request.user).values('username')
obj=obj1 | obj2
friendslist=User.objects.filter(username__in=obj)
Where User is a django User model I am trying to combine two queryset(obj1 and obj2) set here But it's not working.I can do the same thing in sql by using alias .But here i am not sure what to do.
I am getting this error while performing the above code:
TypeError: Merging 'QuerySet' classes must involve the same values in each case
Please help in achieving this task
I think you should do the 'or' in the filter function:
from django.db.models import Q
friendship = FriendsWith.objects.filter(Q(username=request.user)|Q(fusername=request.user))
friendship
is a queryset where the current user can be a username
or a fusername
. You can use that set to get the alternative user that should be their friend.
Another solution is to use django-friendship . It is a good django library for handling friendship and friend requests between users and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.