开发者

group by and aggregate in django

开发者 https://www.devze.com 2023-02-25 00:40 出处:网络
I want to select in django from database. Test have many tests results. I need to select the latest ending开发者_开发百科.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消