Django, division between two annotate result won't calculate correctly

前端 未结 2 679
情话喂你
情话喂你 2021-02-13 23:30

I\'m trying to get a division between two annotate results in queryset. Impression is much larger than click, so I should get tenth decimal.

def get_queryset(se         


        
2条回答
  •  萌比男神i
    2021-02-14 00:04

    As far as I am aware, there isn't a way to do this using the ORM.

    The Sum() function returns the same field type as put into it (i.e. an IntegerField() will always return an Integer). You could use a function like ExpressionWrapper to force the output to be a float, but that won't help in this case as it will be too late: the division of two integers will have been already returning another integer.

    To solve your problem, remove the ctr_monthly section form your query, and create a simple template tag which converts the two numbers to floats and divide them.

    Your template will then look like: {{ monthly_ctr(googleData.click, googleData.impression) | floatformat:2}}

提交回复
热议问题