from django.db.models import Sum, functions, Value class RancherService: @staticmethod def get_total_used_weight(rancher, sale_item, quota_stat): return sale_item.objects.filter( transaction__rancher=rancher, transaction__transaction_status='success', quota_stat=quota_stat, ).aggregate( total_weight=functions.Coalesce(Sum('weight'), Value(0)) )['total_weight']