问题
This is a follow up to my previous question.
I am using the same model
class Item(db.Model):
...
glam = db.StringProperty()
casual = db.StringProperty()
speaking = db.StringProperty()
and assume that I have 2 items and 1 is tagged "glam" the other tagged "speaking".
If I filter like this
query.filter("glam", "glam")
query.filter("speaking"), "speaking")
the filter returns none because this looks for 1 item tagged "glam" and "speaking".
How do I filter for separate items?
Update
Item
table may look like this:
glam speaking
-------------------
item1
item2 glam
item3 speaking
item4
item5 glam
I'd like to filter "glam" and "speaking"
回答1:
With your current schema and the current datastore API, you cannot acheive this with a single query.
If 'glam', 'casual', and 'speaking' are mutually exclusive, you can use an IN
query on a single field:
class Item(db.Model):
...
tag = db.StringProperty()
query.filter("tag IN", ["glam", "speaking"])
You could also use the experimental Datastore Plus API and make an OR query (which is implemented by merging the results of two queries internally):
# Note: Your model must be a Datastore Plus model; this may require some restructuring!
q1 = query.filter("glam =", "glam")
q2 = query.filter("speaking =", "speaking")
for result in q1.OR(q2):
....
Note that the datastore plus API is still in development, so I'm not sure if these queries are run in parallel yet, but they should be eventually. Additionally, since the Datastore Plus API is still experimental and under development, you may have to change your code in response to API flux. You do get to control when these updates occur though, at least.
来源:https://stackoverflow.com/questions/4829736/how-to-filter-for-different-items