Django Query set with clear and exclusive together

I want to send a notification to users who have previously commented on the request. Therefore, for this purpose, I must find individual users and exclude the identifier of the current user (user) from this list.

object_id_list = ScAns.objects.filter(username=username).values_list('id',flat=True)

result-> QuerySet [22]

actionUsers = ScAnsAction.objects.filter(req_id=request_id).values_list('user_id',flat=True).distinct().exclude(id__in=object_id_list)

result-> QuerySet [13, 15, 22]

final The result should not contain 22. It should give [13,15]

+4
source share
1 answer

You must make an exception before calling different ones, as well, to avoid the unnecessary __in, which you can simply exclude based on the username.

actionUsers = ScAnsAction.objects.filter(req_id=request_id)).exclude(username=username).values_list('user_id',flat=True).distinct()
+2
source

Source: https://habr.com/ru/post/1667529/


All Articles