I have this code:
q = MyModel.objects.order_by(\'-value1\').annotate(
res=ExpressionWrapper(
(F(\'value1\') / F(\'value2\')),
Simply make use of F()
's support for multiplication to convert one factor to decimal number.
Combined expression then would look like:
from decimal import Decimal
q = MyModel.objects.order_by('-value1').annotate(
res=ExpressionWrapper(
(F('value1') * Decimal('1.0') / F('value2')),
output_field=FloatField()),
)
I find this more elegant way then write raw SQL CAST on value1 field and then do the division.