The result you expect is very easy to achieve with a raw query, and indeed, I mean it is very difficult to achieve with pure django.
from django.db.models import FloatField, ExpressionWrapper, F template = '%(function)s(%(expressions)s AS FLOAT)' fv1 = Func(F('value1'), function='CAST', template=template) fv2 = Func(F('value2'), function='CAST', template=template) ew = ExpressionWrapper(fv1/fv2, output_field = FloatField()) q = MyModel.objects.order_by('-value1').annotate(res = ew)
You won't blame it for elegance, but it works on both Mysql and Postgresql.
Provide some background. Rounding is done using an integer division database because you have ints. If you want decimal division, you need to drop them by decimal numbers. Unfortunately, with Django, a great drop.
Postgresql has a very elegant way to quit swimming. value1 :: float, but this cannot be used from inside django (at least as far as I know)
source share