A
A
alk2015-12-28 16:13:48
Django
alk, 2015-12-28 16:13:48

How to select elements with maximum value in Django ORM?

Good afternoon!
The database contains data of the form:
name, result
"name 1", 5
"name 2", 2
"name 2", 4
"name 1", 2
You must select the field with the maximum result value for each name .
That is, it should turn out:
"name 1", 5
"name 2", 4
How to do this using Django ORM?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2015-12-28
@Alexey_Kutepov

Model.objects.values('name').annotate(max_name = Max('result')).order_by()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question