TypeError: '>' not supported between instances of 'CombinedExpression' and 'int'
I got this product model:
class Product(models.Model):
quantity = models.DecimalField(max_digits=10, decimal_places=2, default=0)
price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
@property
def value(self):
return F('quantity') * F('price')
when I call .value on product instance I got in return:
product.value // returns 14
but when I check condition:
for product in Product.objects.all():
while (product.value > 0):
...
I got this error:
TypeError: '>' not supported between instances of 'CombinedExpression' and 'int'
I have not found an existing question about this problem. How to solve it?
You work with the attributes directly:
class Product(models.Model):
# …
@property
def value(self):
return self.quantity * self.price
You use F-expressions [Django-doc] to make expressions for database queries, not to multiply values at the Python/Django layer.