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 print actual number when using Aggregate(Max()) in a Django query

I have the following query set:

 max_latitude = Model.objects.aggregate(Max('latitude'))

When I print it, it returns {'latitude__max': 51.6639002} and not 51.6639002.

This is causing a problem when I want to add the latitudes together to calculate an average latitude.

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

If I do print(max_latitude.latitude) (field of Model object) I get the following: error:'dict' object has no attribute 'latitude'

How can I extract the actual number given from the queryset?

>Solution :

Point to the specific field name with

max_latitude = Model.objects.aggregate(Max('latitude'))['latitude__max']

You can also name your field as you want:

max_latitude = Model.objects.aggregate(latitude=Max('latitude'))
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