Horje
divide by zero errors when using annotate Code Example
divide by zero errors when using annotate
# Use NullIf on the Denominator or Divisor
from django.db.models.functions.comparison import NullIf
from django.db.models import F, Avg, Value, FloatField, ExpressionWrapper

context['stock_margin'] = context['top_stock'].annotate(
    Avg_purchase = ExpressionWrapper(
        F('total_puchase') / NullIf(F('quantity_purchase'), 0),
        output_field=FloatField()
    ),
    Avg_sales = ExpressionWrapper(
        F('total') / NullIf(F('quantity'), 0),
        output_field=FloatField()
    )
)




Python

Related
AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ Code Example AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ Code Example
case statement in querset django Code Example case statement in querset django Code Example
read bytes from file python Code Example read bytes from file python Code Example
get most repeated instance in a queryset django Code Example get most repeated instance in a queryset django Code Example
python negation of an statement Code Example python negation of an statement Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7