如何筛选()不同的项目? [英] How to filter() for different items?

查看:147
本文介绍了如何筛选()不同的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的上一个问题



我使用相同的模型

  class Item (db.Model):
...
glam = db.StringProperty()
casual = db.StringProperty()
speaking = db.StringProperty()

并假设我有2个项目,1个标记为glam,另一个标记为发言。



如果我这样过滤

  query.filter(glam,glam )
query.filter(speaking),speaking)

没有返回任何内容,因为这会查找1个标记为glam和speaking的项。



如何过滤单独的项目?

更新

项目表可能看起来像这样:

  glam speaking 
------------ -------
item1
item2 glam
item3 speaking
item4
item5 glam

我想过滤glam和speaking

解决方案

<如果glam,casual和speaking是您可以在一个字段中使用 IN 查询:

  class Item(db.Model):
...
tag = db.StringProperty()

query.filter(tag IN,[glam, ))

您也可以使用实验 Datastore Plus API ,并进行OR查询(通过在内部合并两个查询的结果来实现):

 #注意:您的模型必须是Datastore Plus模型;这可能需要一些重组! 
q1 = query.filter(glam =,glam)
q2 = query.filter(speaking =,speaking)
for q1.OR(q2 ):
....

请注意,数据存储加API仍在开发中,所以我不确定这些查询是并行还是,但它们应该是最终的。此外,由于Datastore Plus API仍处于试验阶段,正在开发中,您可能需要更改代码以响应API流量。你至少可以控制何时发生这些更新。


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"

解决方案

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.

这篇关于如何筛选()不同的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆