Prometheus
Prometheus

Reputation: 33625

Django using aggregate MAX

I have the following...

r = Region.objects.get_location_name(user.location)

Which returns:

[<Region: Great Britain>, <Region: Europe>]

Next I want to only return the object with the MAX highest parent.

I have tried this:

r = Region.objects.get_location_name(user.location).aggregate(Max('level'))

but this does not return an object it returns...

{'level__max': 1}

Why?

Upvotes: 0

Views: 142

Answers (1)

joerick
joerick

Reputation: 16448

You're looking for order_by: Sort the queryset by descending 'level' and then get the first item by doing queryset[0].

I think your code should look like

r = Region.objects.get_location_name(user.location).order_by('-level')[0]

Upvotes: 1

Related Questions