次のように、2 つの新しいフィールド (project_count と member_count) を持つように、Student オブジェクトに注釈を付けました。
top_students = Student.objects.annotate(project_count= Count('project'), member_count = Count('member_student'))
これら 2 つの値の合計をデータベース レベルで実行したいと思います。つまり、次のようなものを返します。
total_count = project_count + member_count
次のように .extra() を使用してみました:
top_students = Student.objects.annotate(project_count= Count('project'), member_count = Count('member_student')).extra(
select = {'total_count': 'project_count + member_count'},
order_by = ('total_count', )
)
しかし、それはエラーを示しています:OperationalError: (1054, "Unknown column 'project_count' in 'field list'")
生のSQLを書くべきですか、それともこれを行う他の方法はありますか: