2👍
✅
highest_bid
is a dictionary that maps the name of the aggregate to a value, so for example { 'bid_input__max': Decimal('14.25') }
, you can thus unwrap the value from the dictionary by subscripting:
highest_bid = Bid.objects.filter(
id=id
).aggregate(Max('bid_input'))['bid_input__max']
but likely this will not give you the expected value. You filter on id=id
, so that means the queryset spans one records. You probably want to filter over the bid_item_id=id
or something similar:
highest_bid = Bid.objects.filter(
bid_item_id=id
).aggregate(Max('bid_input'))['bid_input__max']
based on your comment, it is possible that there is no bid yet. In that case, it will return a None
. We can use zero instead with:
highest_bid = Bid.objects.filter(
bid_item_id=id
).aggregate(Max('bid_input'))['bid_input__max'] or Decimal('0')
Source:stackexchange.com