I am trying to calculate total revenue for a day. Each DailyTotal contains a count of items sold (items_sold) and a Price those items were sold that day (items_price) (every items is sold for the same price all day). That part is working, but now I need to convert multiply that value by the exchange rate for that day/country.
rate = ExchangeRate.objects.filter(
date=OuterRef('date'),
country=OuterRef('country')))
calc_with_rate = Sum(F('items_sold') * F('items_price') * Subquery(rate.values('rate')), output_field=FloatField(),)
results = DailyTotal.objects.filter(**query_filters).annotate(
revenue=calc_with_rate)
but I get:
unsupported operand type(s) for +=: 'int' and 'NoneType
I assume it is because rate.values('rate') is not returning a an int.. but I can't do
rate.values('rate')[0]
or I get:
This queryset contains a reference to an outer query and may only be used in a subquery.
so I am not sure how to complete this query?
rateyou get with theSubqueryand then annotate the calculation foritems_sold * item_price * subquery_result. Then you can sum it.