user469652
user469652

Reputation: 51261

Django: Get other field's value from an aggregation query

es = Employee.objects.filter(a few filters).aggregate(Max('Age'))

That query is going to give me the oldest employee, so I also want to know some other information associated with that row, for example, the id.

How can I accomplish that? Thanks.

Upvotes: 0

Views: 559

Answers (2)

solartic
solartic

Reputation: 4319

Use annotate() instead of aggregate().

Upvotes: 0

VGE
VGE

Reputation: 4191

You do not need aggregate you only have to order by descending Ages your query set

 Employee.objects.filter(a few filters).order_by(['-Age'])[0]

Will give you the oldest

Or if you have several old item

 age = None
 for x in Employee.objects.filter(a few filters).order_by(['-Age']):
    if age and age != x.Age:
       break
    age = x.Age 

Upvotes: 1

Related Questions