Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: get aggregated value of two multiplied columns

I need to get aggregated value of two columns. So first multiple them together and then get theirs sum(). Code below naturally does not work, it is just for clarification.

Is it somehow possible or should I use raw SQL?

SomeModel.objects
    .filter(**something)
    .aggregate(Sum('one_column' * 'another_col'))
like image 838
yedpodtrzitko Avatar asked Dec 09 '09 16:12

yedpodtrzitko


People also ask

How to aggregate in Django?

When specifying the field to be aggregated in an aggregate function, Django will allow you to use the same double underscore notation that is used when referring to related fields in filters. Django will then handle any table joins that are required to retrieve and aggregate the related value.

What is f() in Django?

It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory. Instead, Django uses the F() object to generate an SQL expression that describes the required operation at the database level.

What is annotate in Django?

Appending the annotate() clause onto a QuerySet lets you add an attribute to each item in the QuerySet, like if you wanted to count the amount of articles in each category. However, sometimes you only want to count objects that match a certain condition, for example only counting articles that are published.


2 Answers

You don't need that much raw SQL using extra().

obj = SomeModel.objects.filter(**something).extra(
    select = {'total': 'SUM(one_column * another_column)'},
)
like image 98
rinti Avatar answered Sep 26 '22 01:09

rinti


As I answered here https://stackoverflow.com/a/36024089/4614802 the correct solution depends on django version.

  • For django < 1.8 use .aggregate(Sum('field1', field="field1*field2"))
  • For django >= 1.8 use .aggregate(Sum(F('field1')*F('field2'))
like image 38
Antstud Avatar answered Sep 22 '22 01:09

Antstud