Snowman
Snowman

Reputation: 32071

How to do an OR query in Google App Engine

I have a model called RelatedUsers with has three properties:

firstUserId 
secondUserId 
relationshipStatus

I'm coming from MySQL where I would do this:

mysql_query("SELECT * FROM relatedUsers WHERE (firstUserId='$originatorId' 
AND secondUserId='$recipientId') OR (firstUserId='$recipientId' 
AND secondUserId='$originatorId')");

How can I do something like this with Google App Engine? I can't find any similar cases online..

Upvotes: 1

Views: 106

Answers (1)

Tom Willis
Tom Willis

Reputation: 5303

by running several queries for the keys and then fetching the keys.

from google.appengine.ext import ndb

class User(ndb.Model):
    email = db.StringProperty()


options = ndb.QueryOptions(keys_only=True)
condition_1 = ndb.Query(User, options=options).filter(User.email == "[email protected]")
condition_1 = ndb.Query(User, options=options).filter(User.email == "[email protected]")

key_list = list(set(list(condition_1) + list(condition_2)))

mom_and_dad = ndb.get_multi(key_list)

If you have to order them you will have to do that in memory.

/done from memory :)

Upvotes: 2

Related Questions