How to get maximum value in django ORM

>>> 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)}] 

What would be the best way to get the maximum value here? In python or in ORM?

+8
python django django-models
source share
4 answers
 >>> from django.db.models import Max >>> AuthorizedEmail.objects.all().aggregate(Max('added')) 

And to get the value in the template:

 {{ item.added__max }} 
+22
source share
 AuthorizedEmail.objects.filter(group=group).order_by('-added')[0] 
+4
source share

latest returns the last object in the table according to the date added :

 AuthorizedEmail.objects.filter(group=group).latest('added') 
+2
source share

All Articles