如何使用django ORM对三列求和

Vba*_*Beg 1 django django-models

我想使用djagno模型执行此类查询:

SELECT id, ( first+second+third ) FROM testTable
Run Code Online (Sandbox Code Playgroud)

没有分组.试图使用谷歌找到解决方案,但没有成功.

提前致谢,

oli*_*del 7

使用F()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'),
                       )
Run Code Online (Sandbox Code Playgroud)