在django orm中使用汇总(sum),自定义QuerySet字段

发布于 2025-02-10 20:02:04 字数 186 浏览 0 评论 0原文

我想使用查询,其中我不需要发票和QuerySet中的总发票数量。

Invoice.objects.aggregate(total_amount=Sum('order__order_items__amount')) 
Invoice.objects.count()

如何在单个查询中处理上述查询。

I want to use query in which I need No of Invoices and Total Amount of Invoices in a Queryset.

Invoice.objects.aggregate(total_amount=Sum('order__order_items__amount')) 
Invoice.objects.count()

How can I handle above queries in a single query.

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

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

发布评论

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

评论(1

醉生梦死 2025-02-17 20:02:04

您可以计算 dintiment 主键的数量:

from django.db.models import Count, Sum

Invoice.objects.aggregate(
    total_amount=Sum('order__order_items__amount'),
    number_of_invoices=Count('pk', distinct=True)
)

这将返回使用'total_amount''number_of_invoices'的字典。

You can count the number of distinct primary keys:

from django.db.models import Count, Sum

Invoice.objects.aggregate(
    total_amount=Sum('order__order_items__amount'),
    number_of_invoices=Count('pk', distinct=True)
)

This will return a dictionary with 'total_amount' and 'number_of_invoices' as keys.

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