Django QuerySet 与排除 &注释不返回任何结果

发布于 2024-12-14 18:12:38 字数 1245 浏览 1 评论 0原文

基于这个关于伪造计数的问题键,我有问题。这是我的模型:

class Type(models.Model):
  is_bulk = models.BooleanField()

class Component(models.Model):
  parent = models.ForgeinKey(Type, related_name="components")

我想编写一个具有所有类型的查询集,除了那些具有 is_bulk=True 且没有组件的查询集。如果 is_bulk=False,则应将其包括在内。如果 is_bulk=True 并且您有 1 个以上链接的组件,那么您就被包含在内。如果没有,你就被排除在外。

根据答案,我尝试了这个查询集:

Type.objects.annotate(num_components=Count('components')).exclude(is_bulk=True, num_components=0)

但它没有返回任何结果。

然而,这意味着应该有结果:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0)]

所有 Type 对象都有 is_bulk=False 并且它们都有 0 个组件。通过阅读 .exclude(…) 文档,它应该是 NOT(is_bulk=True AND num_components=0),对于每种类型都应该是 True。正确的? (我在这里是否误解了,如果是这样,正确的查询集是什么)

如果不是,为什么这个查询集返回 [],而它应该返回所有查询集?这是 Django 1.3 中的错误吗?

Based off this SO question about counting forgein keys, I have a problem. This is my models:

class Type(models.Model):
  is_bulk = models.BooleanField()

class Component(models.Model):
  parent = models.ForgeinKey(Type, related_name="components")

I want to write a queryset that has all types, except those that have is_bulk=True and have no components. If is_bulk=False, it should be included. If is_bulk=True and you have 1+ linked Components, then you're included. If not, you're excluded.

Based off the answer, I tried this queryset:

Type.objects.annotate(num_components=Count('components')).exclude(is_bulk=True, num_components=0)

But it returns no results.

However this implies that there should be results:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0)]

All the Type objects have is_bulk=False and all of them have 0 Components. From reading the .exclude(…) documentation, it should be NOT(is_bulk=True AND num_components=0), which should be True for every Type. Right? (Have I made a misunderstanding here, if so, what's the correct queryset)

If not, why is this queryset returning [], when it should return all of them? Is this a bug in Django 1.3?

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

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

发布评论

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

评论(2

红衣飘飘貌似仙 2024-12-21 18:12:45

我尝试了这个例子,我认为你需要的是在你的领域声明一个 lated_name

class Component(models.Model):
    parent = models.ForgeinKey(Type, related_name='components')

应该可以

更新

根据你的例子:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0)]

而我的:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 3), (False, 0), (False, 1), (False, 4), (True, 2), (True, 3), (True, 1), (True, 1), (False, 2), (True, 7), (True, 0), (False, 0)]

我假设你没有符合您的查询集要求的对象:

Type.objects.annotate(num_components=Count('components')).exclude(is_bulk=True, num_components=0)

您的结果:

[]

这是我的情况的结果:

[<Type: Type object>, <Type: Type object>, <Type: Type object>, <Type: Type object>]

I tried this example and what you need I think is declaring a related_name in your field:

class Component(models.Model):
    parent = models.ForgeinKey(Type, related_name='components')

That should do it

UPDATE

Based on your example:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0), (False, 0)]

And mine:

>>> [(x.is_bulk, x.num_components) for x in Type.objects.annotate(num_components=Count('components'))]
[(False, 3), (False, 0), (False, 1), (False, 4), (True, 2), (True, 3), (True, 1), (True, 1), (False, 2), (True, 7), (True, 0), (False, 0)]

I'm assuming you don't have objects with the requirements of your queryset:

Type.objects.annotate(num_components=Count('components')).exclude(is_bulk=True, num_components=0)

Your results:

[]

This are the results in my case:

[<Type: Type object>, <Type: Type object>, <Type: Type object>, <Type: Type object>]
霓裳挽歌倾城醉 2024-12-21 18:12:44

我已经通过将查询集更改为: 解决了这个问题:

types_qs.annotate(num_components=Count('components')).filter(Q(is_bulk=False) | (Q(is_bulk=True) & Q(num_components__gt=0)))

这有效,但我不明白为什么我原来的查询集不起作用。啊好吧。

I have fixed this by changing the queryset to:

types_qs.annotate(num_components=Count('components')).filter(Q(is_bulk=False) | (Q(is_bulk=True) & Q(num_components__gt=0)))

That works, but I can't see why my original one didn't. Ah well.

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