'AttributeError: 'dict' object has no attribute 'annotate field' - django

I'm trying to calculate some fields then group a field, it raise this error :

AttributeError: 'dict' object has no attribute 'total_prices'

here is the query that i tried

collections = Invoice.objects.filter(
    invoice__status=True).annotate(                    
        total_prices=Sum((F('price')) - F('discount'),output_field=DecimalField(max_digits=20,decimal_places=3)),
        paid_prices=Sum(F('cash')),
        total_discount=Sum(F('discount')),
        storage_prices=Sum(
            (F('item__mobile__price')+F('item__mobile__cost')),output_field=DecimalField(max_digits=20,decimal_places=3)),
        incomes=Case(
            When(total_prices__gte=F('storage_prices'),then=F('total_prices')-F('storage_prices')),default=0,output_field=DecimalField(max_digits=20,decimal_places=3)),
        loss=Case(
            When(
                storage_prices__gt=F('total_prices'),then=F('storage_prices') - F('total_prices')),default=0,output_field=DecimalField(max_digits=20,decimal_places=3)),                    
        num=Count('pk'),
        date_collections=TruncDay('item__mobile__collection__collection_date')
        
        ).values('date_collections').order_by('date_collections')
for i in collections:
    print(i.total_prices)

i dont want to put the group by before the annotate( i know it works, but not work as i want for the loss annotate) i also tried this

for i,j in enumerate(collections):
    print(j['total_prices'])

but it raise this error :

KeyError: 'total_prices'

Thank you, most regards ..



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source