开发者

Emulating a "OR" condition in Datastore

开发者 https://www.devze.com 2022-12-30 10:56 出处:网络
I\'m using the Google App Engine with Python (Django). How to emulate \"SELECT * FROM bla WHERE touser = common.userstats(\"key\") OR fromuser = common.userstats(\"key\") ORDER BY date ASC\"?

I'm using the Google App Engine with Python (Django). How to emulate "SELECT * FROM bla WHERE touser = common.userstats("key") OR fromuser = common.userstats("key") ORDER BY date ASC"?

I was thinking of something like this, but I can't get it in the order I want.

    recievedlist = models.P1.all()
    recievedlist.filter("touser =", common.userstats("key")开发者_如何学C)
    plus1list = recievedlist.fetch(50)

    sendlist = models.P1.all()
    sendlist.filter("fromuser =", common.userstats("key"))
    plus1list += sendlist.fetch(50)

    # order plus1list


You could add a ListProperty to your model which contains both touser and fromuser. Then you could execute a single query to retrieve the entities which you are interested in sorted by date. This eliminates a datastore query and the in-memory sort, but costs you an extra index and a little more storage space on your model.

(pseudo) Example:

class bla(db.Model):
    ...
    toandfromuser = db.ListProperty(...)

Then you can do a query like this (since an entity will match if ANY element of the list toandfromuser matches the value you are looking for):

SELECT * FROM bla WHERE toandfromuser = common.userstats("key") ORDER BY date ASC
0

精彩评论

暂无评论...
验证码 换一张
取 消