How to filter() for different items?

半城伤御伤魂 提交于 2020-01-03 05:52:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!