Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
    )
)
Comment

PREVIOUS NEXT
Code Example
Python :: how to set bgcolor of a widget in pyqt5 
Python :: how to ask python function to return something 
Python :: print('Test set predictions: {}'.format(y_pred)) 
Python :: truncate date to midnight in pandas column 
Python :: build spacy custom ner model stackoverflow 
Python :: `12` print () 
Python :: pandas remove prefix from columns 
Python :: media url django 
Python :: python calling dynamic function on object 
Python :: length of list in jinja 
Python :: enumurate in python 
Python :: python print exception type and message 
Python :: python temp directory 
Python :: download from radio javan python 
Python :: python seaborn violin plot fit data better 
Python :: python pandas reading pickelt 
Python :: change size of yticks python 
Python :: YouCompleteMe unavailable: requires Vim compiled with Python (3.6.0+) support. 
Python :: how to create text file with python and store a dictionary 
Python :: how to subtract minutes from time in python 
Python :: only int validator PyQt 
Python :: creating a new folder in python 
Python :: hand tracking module 
Python :: csv python write 
Python :: how to write a font in pygame 
Python :: pip install dal 
Python :: shuffle rows dataframe 
Python :: how to extract zip file in jupyter notebook 
Python :: install selenium python mac anaconda 
Python :: python print time difference 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =