Skip to content Skip to sidebar Skip to footer

Can This Be Achieved Through Aggregation?

I have achieved what I want but I'm not convinced it is the best approach. This is my model: class foo(models.Model): user = models.ForeignKey(User) rate = models.PositiveS

Solution 1:

Based on @9000 comment I manage to solve the problem:

rate = foo.objects.values('user').distinct().annotate(r=Avg("rate"), r1=Avg("rate1"), r2=Avg("rate2")).annotate(a = F('r')+F('r1')+F('r2')).order_by('a')[:5]

The only issue I have now is that it returns the user id, which is useful, but it would be much more convenient if it returned user instead, so I can use it directly in the template.

Thanks,

Post a Comment for "Can This Be Achieved Through Aggregation?"