I'm doing this at the end of an annotation chain on my queryset:
.annotate(diff=F("total_views")/F("previous_views")
The issue is that both total_views and previous_views are annotations themselves. This is working, except when F("previous_views") equals 0. I then get a division by zero error. All attempts to use Case/When have failed.
I'm looking for a way to calculate diff as a fraction, unless previous_views is 0, in which case diff should be None.