模型中的 Django 聚合

发布于 2024-11-09 10:57:44 字数 745 浏览 0 评论 0原文

您好,我正在编写一个网站,该网站将捐赠用作项目中的多个项目。 我正在计算捐款总额,但似乎不起作用。我需要两个模型之间的中间表吗?

我想知道是否有更好的方法来使用 Django Aggregation 来做到这一点。

class Donation(models.Model):
    user = models.ForeignKey(User)
    amount= models.DecimalField(max_digits=12, decimal_places=2)

    def __unicode__(self):
        return self.user

class Project(models.Model):
    name = models.CharField(max_length=200)
    donations=models.ManyToManyField(Donation, null=True, blank=True)

    def __unicode__(self):
        return self.name

    def progress(self):
        donations = self.donations.all()
        total_donations = 0
        for item in donations:
            total_donations += item.amount
        return total_donations

感谢您的热心帮助!

Hi I'm coding a site that uses donations as multiple items on a Project.
I'm calculating the total amount of donations, but it doesn't seem to work. Do i need an intermediate table between the two models?

I was wondering if there's a better to way to do it with Django Aggregation.

class Donation(models.Model):
    user = models.ForeignKey(User)
    amount= models.DecimalField(max_digits=12, decimal_places=2)

    def __unicode__(self):
        return self.user

class Project(models.Model):
    name = models.CharField(max_length=200)
    donations=models.ManyToManyField(Donation, null=True, blank=True)

    def __unicode__(self):
        return self.name

    def progress(self):
        donations = self.donations.all()
        total_donations = 0
        for item in donations:
            total_donations += item.amount
        return total_donations

Thanks for your kind help!

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

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

发布评论

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

评论(2

同展鸳鸯锦 2024-11-16 10:57:44

我将摆脱 progess 方法并简单地注释查询集:

from django.db.models import Sum
Project.objects.annotate(progress=Sum('donations__amount'))

您甚至可以使用自定义 Manager

from django.db.models import Sum

class ProjectManager(models.Manager):
    def all_with_progress(self):
        return self.get_query_set().annotate(progress=Sum('donations__amount'))

class Project(models.Model)
    ...
    objects = ProjectManager()

然后:

>>> projects = Project.objects.all_with_progress()
>>> projects[0].progress
700

或者您甚至可以覆盖默认查询集,因此进度是总是返回:

class ProjectManager(models.Manager):
    def get_query_set(self, *args, **kwargs):
        qs = super(ProjectManager, self).get_query_set(*args, **kwargs)
        return qs.annotate(progress=Sum('donations__amount'))

然后:

>>> projects = Project.objects.all()
>>> projects[0].progress
700

I'd get rid of the progess method and simply annotate the queryset:

from django.db.models import Sum
Project.objects.annotate(progress=Sum('donations__amount'))

You could even use a custom Manager:

from django.db.models import Sum

class ProjectManager(models.Manager):
    def all_with_progress(self):
        return self.get_query_set().annotate(progress=Sum('donations__amount'))

class Project(models.Model)
    ...
    objects = ProjectManager()

Then:

>>> projects = Project.objects.all_with_progress()
>>> projects[0].progress
700

Or you could even override the default queryset so progress is always returned:

class ProjectManager(models.Manager):
    def get_query_set(self, *args, **kwargs):
        qs = super(ProjectManager, self).get_query_set(*args, **kwargs)
        return qs.annotate(progress=Sum('donations__amount'))

Then:

>>> projects = Project.objects.all()
>>> projects[0].progress
700
你曾走过我的故事 2024-11-16 10:57:44
def progress(self):
    from django.db.models import Sum
    amount = self.donations.exclude(amount=None).aggregate(progress=Sum('amount'))['progress']
    if amount:
        return amount
    return 0

我想这会给你你想要的。您必须尝试一下,如果有任何问题请告诉我。另外,请确保您的 Django 版本支持聚合。

编辑:修复了没有返回任何内容的问题。

def progress(self):
    from django.db.models import Sum
    amount = self.donations.exclude(amount=None).aggregate(progress=Sum('amount'))['progress']
    if amount:
        return amount
    return 0

I think that'll give you what you want. You'll have to play with it a bit, and let me know if you have any problems. Also, make sure you version of Django supports aggregation.

EDIT: Fixed a problem where none was being returned.

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