Django Aggregation - The expression contains mixed types. You must set output_field

I am trying to execute Aggregation Query and my code:

TicketGroup.objects.filter(event=event).aggregate(
                           total_group=Sum(F('total_sold')*F('final_price')))

I have "total_sold" and "final_price" in the TicketGroup object, and all I want to do is add and multiply values ​​to get the total sales of all TicketGroups together.

All I get is an error:

The expression contains mixed types. You must set output_field

What am I doing wrong since I call "total_group" as my output field?

Thank!

+10
source share
2 answers

output_field Django Sum.

from django.db.models import FloatField, F
total_group=Sum(F('total_sold')*F('final_price'), output_field=FloatField())

.

+22

- , . output_field . . .

from django.db.models import FloatField, ExpressionWrapper, F

distinct_people_with_more_than_zero_bill = Task.objects.filter(
    billable_efforts__gt=0).values('report__title').annotate(
    Count('assignee', distinct=True)).annotate(
    Sum('billable_efforts'))

annotate(yy=ExpressionWrapper(F('billable_efforts__sum') / F('assignee__count'), output_field=FloatField()))

- ExpressionWrapper. :

Django, :

, , , Django, . F() output_field, ExpressionWrapper

: https://docs.djangoproject.com/en/2.2/ref/models/expressions/

0

Source: https://habr.com/ru/post/1648889/


All Articles