Brug betingede udtryk :
from django.db.models import Case, F, Sum, When
Stats.objects.values('product').annotate(
tot_impressions=Sum('impressions'),
tot_clicks=Sum('clicks')
).annotate(
ctr=Case(When(tot_impressions=0, then=None), # or other value, e.g. then=0
# 1.0*... is to get float in SQL
default=1.0*F('tot_clicks')/F('tot_impressions'),
output_field=models.FloatField())
).order_by('ctr')