如何获得QuerySet不dict django汇总

发布于 2025-02-11 05:45:54 字数 331 浏览 0 评论 0原文

我正在使用此QuerySet作为我的要求,

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

该要求返回dict --->

{'total_amount': Decimal('17700.00'), 'number_of_invoices': 2}

但是我想要QuerySet我该怎么做。 感谢您的帮助

i'm using this queryset for my requirements

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

which returns dict--->

{'total_amount': Decimal('17700.00'), 'number_of_invoices': 2}

but I want queryset how can i do that..
Thanks for any help

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

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

发布评论

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

评论(2

倚栏听风 2025-02-18 05:45:54

这就是汇总的操作:)

您可以将Decimalfield添加到发票模型,在每次保存之前汇总total_amount。我建议这样做的信号。

apps.py
class YourAppConfig(AppConfig):
    ...

    def ready(self):
        from . import signals
Signals.py
from django.db.models.signals import pre_save
from django.dispatch import receiver

from .models import Invoice

@receiver(pre_save, sender=Invoice)
def total_amount_aggregation(sender, instance, *args, **kwargs):
    instance.total_amount = # aggregate desired amount here

您必须为order设置类似的信号,因此,如果您更改order对象,相关Invoice对象将是重新计算。

之后,querySet将保持为querySet,每个对象都将始终具有caclated字段。

This is what aggregate do :)

You can add a DecimalField to Invoice model, that aggregates total_amount during before every save. I suggest signals for that.

apps.py
class YourAppConfig(AppConfig):
    ...

    def ready(self):
        from . import signals
signals.py
from django.db.models.signals import pre_save
from django.dispatch import receiver

from .models import Invoice

@receiver(pre_save, sender=Invoice)
def total_amount_aggregation(sender, instance, *args, **kwargs):
    instance.total_amount = # aggregate desired amount here

You have to set similar signals for your Order, so if you change Order object, the related Invoice object will be recalculated.

After that QuerySet will stay as QuerySet and every object will have always caclulated field.

而不是使用 .Aggregate(...) 使用 .annotate(...) 这将返回给定表达式的 querySet

from django.db.models import Sum, Count

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

然后要获得 sum(...)< /code>&amp; count(...) field of this 发票[0] .total_amount__sum&amp; 发票[0] .number_of_invoices__count

Instead of using .aggregate(...) use .annotate(...) this will return a QuerySet with given expression

from django.db.models import Sum, Count

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

then to get Sum(...) & Count(...) of field do like this invoices[0].total_amount__sum & invoices[0].number_of_invoices__count

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