How to get access to a different object through a variable storing Django Max object?

I have a Django model called Bids with the following objects: listing, bid, and user. I am trying to get access to the user with the largest bid. How do you do that?

I am currently trying:

winning_bid = Bids.objects.aggregate(Max('bid'))
winner = winning_bid.user

>Solution :

You can obtain the Bids with the largest bid with .latest(…) [Django-doc]:

winning_bid = Bids.objects.latest('bid')
winner = winning_bid.user

You can boost efficiency with .select_related(…) [Django-doc] to load the user details in the same query:

winning_bid = Bids.objects.select_related('user').latest('bid')
winner = winning_bid.user

Leave a Reply