过滤标签列表

发布于 2024-12-02 10:00:36 字数 513 浏览 2 评论 0原文

我正在尝试选择 Django 数据库中标签为给定列表中任意标签的所有歌曲。有一个 Song 模型、一个 Tag 模型和一个 SongTag 模型(用于多对多关系)。

这是我的尝试:

taglist = ["cool", "great"]
tags = Tag.objects.filter(name__in=taglist).values_list('id', flat=True)
song_tags = SongTag.objects.filter(tag__in=list(tags))

此时我收到一个错误:

DatabaseError: MultiQuery does not support keys_only.

我做错了什么?如果您能提出一种完全不同的方法来解决问题,我们也会非常欢迎!

编辑:我应该提到我正在 Google AppEngine 上使用 Django 和 django-nonrel

I'm trying to select all the songs in my Django database whose tag is any of those in a given list. There is a Song model, a Tag model, and a SongTag model (for the many to many relationship).

This is my attempt:

taglist = ["cool", "great"]
tags = Tag.objects.filter(name__in=taglist).values_list('id', flat=True)
song_tags = SongTag.objects.filter(tag__in=list(tags))

At this point I'm getting an error:

DatabaseError: MultiQuery does not support keys_only.

What am I getting wrong? If you can suggest a completely different approach to the problem, it would be more than welcome too!

EDIT: I should have mentioned I'm using Django on Google AppEngine with django-nonrel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你对谁都笑 2024-12-09 10:00:36

您不应该将 m2m 关系与 AppEngine 结合使用。 NoSQL 数据库(BigTable 就是其中之一)通常不支持 JOIN,程序员应该对数据结构进行非规范化。这是一个经过深思熟虑的设计决定:虽然你的数据库将包含冗余数据,但你的读取查询会更简单(不需要组合来自3个表的数据),这反过来又使数据库服务器的设计变得更简单(当然这是为了优化和扩展而创建的)

在您的情况下,您可能应该摆脱 Tag 和 SongTag 模型,并将标签作为字符串存储在 Song 模型中。我当然假设Tag模型只包含id和name,如果Tag实际上包含更多数据,你仍然应该有Tag模型。在这种情况下,歌曲模型应包含 tag_id 和 tag_name。正如我上面所解释的,这个想法是为了更简单的查询而引入冗余

You shouldn't use m2m relationship with AppEngine. NoSQL databases (and BigTable is one of them) generally don't support JOINs, and programmer is supposed to denormalize the data structure. This is a deliberate design desicion: while your database will contain redundant data, your read queries will be much simpler (no need to combine data from 3 tables), which in turn makes the design of DB server much simpler as well (of course this is made for the sake of optimization and scaling)

In your case you should probably get rid of Tag and SongTag models, and just store the tag in the Song model as a string. I of course assume that Tag model only contains id and name, if Tag in fact contains more data, you should still have Tag model. Song model in that case should contain both tag_id and tag_name. The idea, as I explained above, is to introduce redundancy for the sake of simpler queries

故人爱我别走 2024-12-09 10:00:36

请让 ORM 为您构建查询:

song_tags = SongTag.objects.filter(tag__name__in = taglist)

Please, please let the ORM build the query for you:

song_tags = SongTag.objects.filter(tag__name__in = taglist)
穿透光 2024-12-09 10:00:36

您应该尝试仅使用一个查询,以便 Django 也仅使用连接生成一个查询。
像这样的东西应该有效:

Song.objects.filter(tags__name__in=taglist)

您可能需要更改此示例中的一些名称(最有可能是 tags__name__in 中的 tags),请参阅 https://docs.djangoproject.com/en/1.3/ref/models/relations/

You should try to use only one query, so that Django also generates only one query using a join.
Something like this should work:

Song.objects.filter(tags__name__in=taglist)

You may need to change some names from this example (most likely the tags in tags__name__in), see https://docs.djangoproject.com/en/1.3/ref/models/relations/.

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