def get_with_total(cls): # запрос, так быстрее total_output_field = models.DecimalField(max_digits=10, decimal_places=2) total_expr = E(F('amount') * F('product__price'), output_field=total_output_field) return cls.objects.annotate(total=total_expr)
def order_value(self): queryset = self.salesorderitem_set.aggregate(order_value=Sum( E(F('unit_price') * F('quantity_ordered'), output_field=models.DecimalField())), ) return queryset['order_value'] or 0
def get_with_total(cls): total_output_field = models.DecimalField(max_digits=10, decimal_places=2) total_expr = E(F('qty') * F('products__price'), output_field=total_output_field) return cls.objects.annotate(total=total_expr)