Django - Annotating Weighted AVG by Group

天大地大妈咪最大 提交于 2019-12-10 18:32:51

问题


I've got the following model manager that operates on a SalesRecord:

def by_variety_and_date(self, start_date, end_date):
    return self.model.objects.filter(
        date__range=(start_date, end_date)
    ).values(
        "variety"
    ).annotate(
        qty_applied=Sum('qty_applied'),
        margin=Avg('margin')
    )

What I'd really like is for the margin=Avg('margin') to return a weighted average, based on qty_applied. Is there a way to do this with Django's annotate/aggregate queries? I've been experimenting with stringing .aggregate() to the end of this, but I still want the average per variety as described by this queryset.

The model in this case looks like this:

class Sale(models.Model):
    margin = models.DecimalField(null=True, blank=True, decimal_places=2, max_digits=12)
    qty_applied = models.IntegerField(null=True, blank=True)
    variety = models.ForeignKey(Variety, null=True)
    totals = Totals()

EDIT

This is what I finally ended up with. It's a bit wonky, but it does the trick.

def by_variety_and_date(self, start_date, end_date):
    return self.model.objects.filter(
        date__range=(start_date, end_date)
    ).values(
        "variety"
    ).annotate(
        qty_applied=Sum('qty_applied'),
        profit=Sum('profit'),
        cogs=Sum('cogs'),
        sales=Sum('value'),
        margin=Sum(
            (F('qty_applied')*F('margin')), output_field=FloatField()
        ) / Sum(
            'qty_applied', output_field=FloatField())
    )

I used the F objects as @WTower suggested to multiply each object's margin by it's qty_applied, then wrapped the whole thing in a Sum and divided it by the Sum of the qty_applied of all objects in the group. Works like a charm!


回答1:


You can use the F() expression

>>> from django.db.models import F, FloatField, Sum
>>> MyModel.objects.all().aggregate(
...    w_avg=Sum((F('a')*F('b'))/F('b'), output_field=FloatField()))

More on aggregation here.

Alternatively, you can use custom SQL (not recommended) or a custom method in the manager.



来源:https://stackoverflow.com/questions/38758372/django-annotating-weighted-avg-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!