如何在django manytomanyfield上应用过滤器,以使该场遵循该条件的多个值?

发布于 2025-02-08 20:52:43 字数 736 浏览 2 评论 0原文

class Publication(models.Model):
    title = models.CharField(max_length=30)


class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

p1 = Publication.objects.create(title='The Python Journal')
p2 = Publication.objects.create(title='Science News')
p3 = Publication.objects.create(title='Science Weekly')

我想过滤文章,并在p1p3上发表。他们可能会或可能不会在其他出版物中发布,但必须在p1p3中发布。

我尝试过:

Article.objects.filter(Q(publications=p1) & Q(publications=p3))

但是它返回空的queryset,这是不正确的

class Publication(models.Model):
    title = models.CharField(max_length=30)


class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

p1 = Publication.objects.create(title='The Python Journal')
p2 = Publication.objects.create(title='Science News')
p3 = Publication.objects.create(title='Science Weekly')

I want to filter articles, published in both p1 and p3. They might or might not publish in other publications but they must have to publish in p1 and p3.

I have tried:

Article.objects.filter(Q(publications=p1) & Q(publications=p3))

but it returns empty queryset which is not true

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

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

发布评论

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

评论(3

忘羡 2025-02-15 20:52:44

如果将mutliple链接到filter()他们的行为应该像与相连的行为:

articles = Article.objects.filter(publications=p1).filter(publications=p3).distinct()

If you chain mutliple calls to filter() they should behave like being connected with an AND:

articles = Article.objects.filter(publications=p1).filter(publications=p3).distinct()
年少掌心 2025-02-15 20:52:44

在多对多和一对一的测试中,有许多foo__in = ...样式过滤器的示例。这是针对您特定问题的语法:

# filtering on a few publications, by id
articles = Article.objects.filter(publications__in=[<id1>, <id2>, <id3>])
# and by publication object (object gets converted to pk under the covers)
articles = Article.objects.filter(publications__in=[publication1, publication2, publication3])
# and by publication title
articles = Article.objects.filter(publications__title__icontains='Science')

icontains用于i的case-costemintimentsmitive,包含该单词包含这些字母,也许还有较少或更多字母。

使用 noreflow noreferrer“> querySet)

There are many examples of FOO__in=... style filters in the many-to-many and many-to-one tests. Here is syntax for your specific problem:

# filtering on a few publications, by id
articles = Article.objects.filter(publications__in=[<id1>, <id2>, <id3>])
# and by publication object (object gets converted to pk under the covers)
articles = Article.objects.filter(publications__in=[publication1, publication2, publication3])
# and by publication title
articles = Article.objects.filter(publications__title__icontains='Science')

The icontains used to i for case-insensitive, contains for that the word contains those letters and maybe there are fewer or more letters.

The double underscore (__) syntax is used all over the place when working with queryset.

两人的回忆 2025-02-15 20:52:44

我遇到了一个类似的问题,有些用户以自由角色和付费角色结尾,因此我需要选择在许多领域中具有两个特定角色的用户。

我做了这样的事情,就像魅力一样

roles = [premium_role, default_role]

DiscordUser.objects.filter(guild=guild)
    .annotate(freemium=Count('roles', filter=Q(roles__in=roles)))
    .filter(freemium=2)
    .values('id')

I had a similar problem where some users ended with a free role and also a paid role, so I needed to select users that have two specific roles on the many to many field.

I did something like this, works like a charm

roles = [premium_role, default_role]

DiscordUser.objects.filter(guild=guild)
    .annotate(freemium=Count('roles', filter=Q(roles__in=roles)))
    .filter(freemium=2)
    .values('id')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文