您可以尝试使用条件语句:
from django.db.models import Count, Max, Case, When, FloatField, F
MyObject.objects.all().annotate(count=Count('audits')).annotate(
max=Case(
When(count=0, then=F('weight')),
When(count__gt=0, then=Max('audits__auditor_weight')),
output_field=FloatField()
)
)
).order_by('max')
潇潇雨雨
相关分类