【问题标题】:Count attributes of many to many relation and filter by it计算多对多关系的属性并按它过滤
【发布时间】:2016-01-12 01:12:39
【问题描述】:

我想做一些与this 稍有不同的事情。

假设我的 models.py 中有这样的东西:

class Hipster(models.Model):
  name = CharField(max_length=50)
  has_iphone = BooleanField(default=True)

class Party(models.Model):
  participants = models.ManyToManyField(Hipster, related_name="participants")

然后做:

hip_parties = Party.objects.filter(participants__has_iphone__istrue__count=4)

我该怎么做?

更新:

>>> Question.objects.filter(options__is_correct=True).annotate(options__count=Count('options')).filter(options__count=0)
[]
>>> q = Question.objects.get(id=49835)
>>> q.options.all()[0].is_correct
False
>>> q.options.all()[1].is_correct
False
>>> q.options.all()[2].is_correct
False
>>> q.options.all()[3].is_correct
False
>>> q.options.all()[4].is_correct
False
>>> q.options.all()[5].is_correct
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/patrickbassut/Programming/logus/lib/python2.7/site-packages/django/db/models/query.py", line 177, in __getitem__
    return list(qs)[0]
IndexError: list index out of range

【问题讨论】:

  • 你可以简单地使用像 Party.objects.filter(participants__has_iphone).count() 这样的计数,然后你就可以完成剩下的逻辑了
  • 我认为这行不通。
  • 您与个人资料有多对多的关系。 Profile 是否有 has_iphone 字段?
  • 抱歉,刚刚更正了。它应该是Hipster

标签: django django-models


【解决方案1】:

您可以为此使用注释。

from django.db.models import Count
Party.objects.filter(
    participants__has_iphone=True
).annotate(iphone_count=Count('participants')).filter(
    iphone_count=4
)

【讨论】:

  • 你不是只过滤至少有一个时髦人士使用 iPhone 的派对吗?我的意思是,如果参与者中只剩下那些是真实的,那就太好了。但我想事实并非如此。
  • 是的,但这不是您想要的吗?你想要有 4 位时髦人士使用 iphone 的派对。因此,用 iphone 过滤掉没有潮人的派对是正确的行为。
  • 上面的查询获取所有至少有一个为真的缔约方。我想要那些正好有 4 个真实的。你甚至可以删除你的第一个过滤器,它不会有任何区别。
  • 不,这根本不是真的。请参阅order of annotate and filter clauses 上的文档 - 第一个过滤器将注释限制为拥有 iphone 的参与者,注释将这些参与者计算在内,第二个过滤器将整个事件限制为有四个使用 iphone 的时髦人士。
  • 我在这里测试过,你是对的。我有这个印象是因为即使我只过滤了那些有 4 位时髦人士使用 iphone 的人,它仍然会显示那些不在 manytomany 上的人。但这是完全正确的。那时我不需要打开那个问题。非常感谢您打开我的眼睛。
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多