Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading