查询数据存储中包含列表属性中特定元素的所有模型

发布于 2024-10-13 07:48:42 字数 950 浏览 4 评论 0原文

嘿,我有一个这样的模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-20 07:48:42

哎呀,我解决了,抱歉打扰了!对于任何感兴趣的人,您都可以使用如下查询:

SELECT * FROM List where categories = 'Philosophy'

它将匹配类别中包含“哲学”的任何列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文