2

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'?

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Praveen
  • 2,400
  • 3
  • 23
  • 30

2 Answers2

3

Fix:

UserAccount.objects.filter(Q(id = friends[0]) | Q(id = friends[1]))
Leonardo.Z
  • 9,425
  • 3
  • 35
  • 38
  • 1
    '&' and '|' doesn't work... see: http://stackoverflow.com/questions/8189702/mongodb-using-an-or-clause-in-mongoengine – Praveen Nov 05 '13 at 07:15
  • The answer in the SO you just posted is out of date, and I don't think it was right even at the first place. You can't override `and` `or` behavior in Python, but you can override `|` ,`&` – Leonardo.Z Nov 05 '13 at 07:23
  • result = UserAccount.objects( Q(first_name__istartswith = query[0]) and Q(last_name__istartswith = query[1])).only('first_name', 'last_name', 'user_name') i'm using this query too, here 'and' works fine whereas '&' doesn't work – Praveen Nov 05 '13 at 07:25
  • @Praveen But is the result as same as `result = UserAccount.objects( Q(first_name__istartswith = query[0])).only('first_name', 'last_name', 'user_name')` ? – Leonardo.Z Nov 05 '13 at 07:27
  • No it has some more users added up to the result – Praveen Nov 05 '13 at 07:32
  • @Praveen I made a mistake, compare the result to `UserAccount.objects( Q(last_name__istartswith = query[1])).only('first_name', 'last_name', 'user_name') ` – Leonardo.Z Nov 05 '13 at 07:37
  • @Praveen `X and Y` equals `Y` when X and Y are both true in Python(in most other languages too) – Leonardo.Z Nov 05 '13 at 07:47
  • So how do i fix this query to produce the expected result? when '&' '|' doesn't seem to work... – Praveen Nov 05 '13 at 07:48
  • @Praveen Can you get the correct result if you change `id` to another field(just for test)? – Leonardo.Z Nov 05 '13 at 08:06
  • @Praveen What does `What does (Q(id = friends[0]) | Q(id = friends[1])).to_query('UserAccount')` produce? It should produce something like `{'$or': [{'_id': ObjectId('5278b9b61dc689c0a2689ae3')}, {'_id': ObjectId('5278b9b81dc689c0a2689ae4')}]}`. Does mongodb return a result if you manually execute the query in mongo shell? – Leonardo.Z Nov 05 '13 at 09:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/40553/discussion-between-praveen-and-leonardo-z) – Praveen Nov 05 '13 at 10:06
3

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

UserAccount.objects.filter(id__in=friends)
Ross
  • 17,861
  • 2
  • 55
  • 73