django 聚合或注释

发布于 2024-12-25 02:08:48 字数 324 浏览 2 评论 0原文

我知道这是一件非常愚蠢的事情,但我似乎不了解 Django 聚合和注释函数。

我有一组非常简单的模型:EventsAreasTypes。事件具有指向 AreaType 的外键。我只想知道任何区域即将发生的事件的数量以及任何类型都相同的数量,即 Area1 - 5 即将发生的事件Area2 - 6Type1 - 34 个事件 等等。

我想避免编写自定义 SQL 和 q 运算符(如果可能的话)。

This is a very stupid thing, I know, but I just don't seem to get the handle on Django aggregate and annotate functions.

I have a very simple set of models: Events, Areas and Types. An event has foreign keys pointing to Area and Type. I would simply like to have the number of forthcoming events for any area and the same for any type, i.e. Area1 - 5 forthcoming events, Area2 - 6, or Type1 - 34 events and so on.

I would like to avoid writing custom SQL, and the q operator if possible.

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

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

发布评论

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

评论(4

孤芳又自赏 2025-01-01 02:08:48

对于给定区域:

my_area = Area.objects.all()[0]
Event.objects.filter(area=my_area).count()

注释

events = Event.objects.annotate(Count('area'))
for event in events:
    print event, event.area__count

events = Event.objects.annotate(count=Count('area'))
for event in events:
    print event, event.count

请参阅以下文档:

https:// docs.djangoproject.com/en/dev/ref/models/querysets/#annotate

for a given area:

my_area = Area.objects.all()[0]
Event.objects.filter(area=my_area).count()

annotation

events = Event.objects.annotate(Count('area'))
for event in events:
    print event, event.area__count

or

events = Event.objects.annotate(count=Count('area'))
for event in events:
    print event, event.count

See the following docs:

https://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate

萌梦深 2025-01-01 02:08:48

如果您只需要单个区域的事件总数,则不需要 annotateaggregate,一个简单的 count 就可以了:

Event.objects.filter(area=my_area).count()

如果您想要多个区域的事件计数,则需要将 annotatevalues 结合使用:

Event.objects.values('area').annotate(Count('area'))

If you just need the total number of events for a single area, you don't need either annotate or aggregate, a simple count will do:

Event.objects.filter(area=my_area).count()

If you want the count of events for multiple areas, you need annotate in conjunction with values:

Event.objects.values('area').annotate(Count('area'))
随梦而飞# 2025-01-01 02:08:48

非常感谢大家。我遇到的问题记录在上一个版本中,它与注释和过滤器优先级有关。

areas = Area.objects.filter(event__in = eventQuery).annotate(num=Count('event'))

我的错误在于我首先进行注释,然后进行过滤。

Thank you all very much. The problem I was having is documented in the last version, it is about the annotate and filter precedence.

areas = Area.objects.filter(event__in = eventQuery).annotate(num=Count('event'))

My error was in the fact that I was doing annotate first and filter second.

究竟谁懂我的在乎 2025-01-01 02:08:48

给定模型事件和区域为:

from django.db.models import Count

class Area(models.Model):
    area_name = models.CharField(...)
    address = models.CharField(...)


class Event(models.Model):
    event_name = models.CharField(...)
    area = models.ForeignKey(Area,...)

为了获得每个区域中的事件总数,您可以执行以下操作:

area_query = Area.objects.filter(yourfilter)
total_event_per_area = Event.objects.filter(area__in=area_query).values('area').annotate(Count('id'))
print(total_event_per_area)
<QuerySet [{'area': 2, 'id__count': 2}, {'area': 4, 'id__count': 3}]>

Given the models Event and Area as:

from django.db.models import Count

class Area(models.Model):
    area_name = models.CharField(...)
    address = models.CharField(...)


class Event(models.Model):
    event_name = models.CharField(...)
    area = models.ForeignKey(Area,...)

In order to get the total number of events in each area, you can do:

area_query = Area.objects.filter(yourfilter)
total_event_per_area = Event.objects.filter(area__in=area_query).values('area').annotate(Count('id'))
print(total_event_per_area)
<QuerySet [{'area': 2, 'id__count': 2}, {'area': 4, 'id__count': 3}]>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文