在 django 使用统计 annotate 和 aggregate 时由于没有数据导致统计是 null , 而不是 0.
这对返回结果产生很大影响, 可以使用 Coalesce 方法来设置默认值.
1 2 3 4 5 6 7 8 9 10 11
   | from django.db.models import Sum, Value as V from django.db.models.functions import Coalesce
  >>>  >>> aggregated = Author.objects.aggregate( ...    combined_age=Coalesce(Sum('age'), V(0)), ...    combined_age_default=Sum('age')) >>> print(aggregated['combined_age']) 0 >>> print(aggregated['combined_age_default']) None
   | 
 
参考链接