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
精彩评论