我正在使用DRF开发一个django rest api,在我的一个视图中,我覆盖了get_queryset函数:
class UserSearchListView(generics.ListAPIView):
...
def get_queryset(self):
current_user_friends = Friend.objects.friends(self.request.user)
all_users = User.objects.all()
# from the all_users queryset I need to remove the current_user_friends queryset.
# Should the exclude function be used?
从all_users
查询集中,我需要排除发出请求的用户和current_user_friends
查询集中包含的同一用户的朋友。我该怎么做?在
您可以利用values_list和QuerySets的.exclude()方法来实现您的愿望:
要排除发出请求的用户,可以从请求对象中检索该用户,或者在请求中传递一个标识值来检索该用户,并将其包含在单独的.exclude()中。在
比如:
相关问题 更多 >
编程相关推荐