2017-07-28 8 views
0

私はDRFを使用してジャンゴ残りのAPIを開発していますし、私のビューのいずれかで私がget_queryset機能オーバーライドします:私はその要求と同じユーザの友人を作り、ユーザーを除外する必要がall_usersクエリセットからdjangoの全ユーザからクエリーセットを除外する方法は?

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? 

をクエリーセットcurrent_user_friendsに含まれています。これどうやってするの?

答えて

0

何かのように:あなたができる、

current_friends_excluded = all_users.exclude(id__in=current_user_friends.values_list('id', flat=True)) 

が要求を行ったユーザを除外するには:

all_users = [1,2,3,4] 
current_user_friends = [1,2] 
print list(set(all_users)-set(current_user_friends)) 
print [user for user in all_users if user not in current_user_friends] 
print filter(lambda user: user not in current_user_friends, all_users) 
+0

実際には、すべてのクエリセットはリストではありません。申し訳ありません。 –

+0

quertsetsをリストに変換して戻すことはできますか? – dashiell

+0

おそらく、もっと良い方法があります。 –

0

あなたはあなたが望むものを達成するためにvalues_listとクエリセットの.exclude()メソッドを活用することができますそれを要求オブジェクトから取り出したり、要求内の識別値を渡してそれを取得したり、別の.exclude()にインクルードしたりすることができます。

関連する問題