11
>>> AuthorizedEmail.objects.filter(group=group).values('added')
[{'added': datetime.datetime(2012, 5, 19, 13, 8, 7)}, 
{'added': datetime.datetime(2012, 5, 19, 13, 8, 7)}, 
{'added': datetime.datetime(2012, 5, 19, 13, 7, 23)}, 
{'added': datetime.datetime(2012, 5, 19, 13, 8, 7)}]

在这里获得最大值的最佳方法是什么?在 python 中还是在 ORM 中?

4

4 回答 4

31

文档中:

>>> from django.db.models import Max
>>> AuthorizedEmail.objects.aggregate(Max('added'))

并获取模板中的值:

{{ item.added__max }}
于 2012-05-19T20:23:34.710 回答
8

latest根据added日期返回表中最新的对象:

AuthorizedEmail.objects.filter(group=group).latest('added')
于 2013-12-13T20:51:09.530 回答
5
AuthorizedEmail.objects.filter(group=group).order_by('-added')[0]
于 2012-05-19T22:18:51.097 回答