سؤال

I have a list of objectIDs of users

friends = ['someID', 'someID']

I wrote a queryset to get the users associated with these objectID that I have in friends list.

u = UserAccount.objects.filter(Q(id = friends[0]) or Q(id = friends[1]))

now u.to_json() outputs only one UserAccount object though both the object ID exists in database

So I checked it by separating the query, now each queryset returned a UserAccount object as expected.

u = UserAccount.objects.filter(Q(id = friends[0]))
v = UserAccount.objects.filter(Q(id = friends[1]))

What could possibly be wrong? Is there some issue with the 'or'?

هل كانت مفيدة؟

المحلول

To match any in a list you can use the in query operator:

UserAccount.objects.filter(id__in=friends)

نصائح أخرى

Fix:

UserAccount.objects.filter(Q(id = friends[0]) | Q(id = friends[1]))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top