Getting value from query set in django views.py

I have a django view:

order_amount = Item.objects.filter(order_id = order_id).values(‘order_id’).annotate(order_value=Sum(‘total’)).order_by(‘order_id’)

which generates the following QuerySet:

<QuerySet [{‘order_id’: 46, ‘order_value’: Decimal (‘140137.20’)} ]>

How do i get only the order_value from the QuerySet?

I tried the following:’
for order in order_amount:
order_value = order.order_value
print(order_value)

but an error was returned:
‘dict’ object has no attribute ‘order_value’

Guidance please.

The values function returns a dictionary, not the objects. (Take particular note of the first example in the docs.) This means your references to the values within that dict are by dictionary key, not object reference:
order_value = order['order_value']

1 Like

Noted. Thanks a lot.