group by and aggregate in django
I want to select in django from database.
Test have many tests results.
I need to select the latest ending开发者_开发百科.
in SQl I would use
select *,max(timeEnd) from testresult group by idTest
and receive a good answer how can i make something alike in django
thanks in advance.
Aggregation is relatively simple in Django. Here is the documentation on the topic:
http://docs.djangoproject.com/en/dev/topics/db/aggregation/
To achieve your query in Django, you'd probably write something like:
from django.db.models import Max
TestResult.objects.values('idTest').annotate(Max('timeEnd'))
You could even use raw sql if it is becoming more complex check this
精彩评论