1

I'd like to execute such query using djagno models:

SELECT id, ( first+second+third ) FROM testTable

Without grouping. Was trying to find the solution using google but unsuccessfully.

Thanks in advance,

1 Answer 1

8

Use F() and annotate():

from django.db.models import F

# the sum of the three columns `first`, `second` and `third` will be
# accessible as `.my_sum`
results = my_model.objects.annotate(
                         my_sum=F('first') + F('second') + F('third'),
                       )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.