查询数据存储中包含列表属性中特定元素的所有模型
嘿,我有一个这样的模型:
class List(db.Model):
user = db.ReferenceProperty(User)
listname = db.StringProperty()
published = db.DateTimeProperty(auto_now_add=True)
score = db.IntegerProperty(required=False)
tld = db.StringProperty(required=False)
categories = db.StringListProperty()
其中列表可以附加多个类别(因此是列表属性)。我想使用这些类别来构建类别页面,因此我需要一个查询来获取其类别属性中具有特定类别的所有列表。我尝试了几种不同的方法,但似乎都不起作用。有这样的查询吗? (以下不起作用):
select * from List where 'Philosophy' in categories
如果没有,我将不得不做类似的事情:
lists = List.all()
for list in lists:
if 'Philosophy' in list.categories:
#add this list to the lists to display on page
但这感觉要么会非常慢,要么会以某种方式破坏......
有什么想法吗?谢谢!
汤姆
更新:
哎呀,我解决了,抱歉打扰了!对于任何感兴趣的人,您都可以使用如下查询:
SELECT * FROM List where categories = 'Philosophy'
它将匹配类别中包含“哲学”的任何列表。
Hey, I have a model like this:
class List(db.Model):
user = db.ReferenceProperty(User)
listname = db.StringProperty()
published = db.DateTimeProperty(auto_now_add=True)
score = db.IntegerProperty(required=False)
tld = db.StringProperty(required=False)
categories = db.StringListProperty()
Where a list could have multiple categories attached to it (hence the listproperty). I want to use these categories to build category pages so I need a query I can use to fetch all lists that have a particular category within their categories attribute. I've tried a few different approaches and none seem to work. Is there a query like this this? (the following doesn't work):
select * from List where 'Philosophy' in categories
If not, I'm going to have to do something like:
lists = List.all()
for list in lists:
if 'Philosophy' in list.categories:
#add this list to the lists to display on page
But this feels like it would be either incredibly slow or break somehow...
Any ideas? Thanks!
Tom
UPDATE:
Oops, I solved it sorry for the bother! For anyone who's interested you can just use a query like this:
SELECT * FROM List where categories = 'Philosophy'
Which will match any list which has 'Philosophy' within the categories.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀,我解决了,抱歉打扰了!对于任何感兴趣的人,您都可以使用如下查询:
它将匹配类别中包含“哲学”的任何列表。
Oops, I solved it sorry for the bother! For anyone who's interested you can just use a query like this:
Which will match any list which has 'Philosophy' within the categories.