[Django]-AttributeError: 'CombinedExpression' object has no attribute 'default_alias'

12👍

You have not assigned a column name to your combined expression.

The output of the annotate() clause is a QuerySet; this QuerySet can be modified using any other QuerySet operation. So every value needs to be in a column and every column needs a name (which you forgot to add)

Do

Product.objects.annotate(in_stock_quantity=Sum('producttransactiondetails__purchase_quantity') - Sum('producttransactiondetails__sales_quantity'))

Mind the in_stock_quantity= here

Leave a comment