Reputation: 110083
>>> 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 max value here? In python or in the ORM?
Upvotes: 12
Views: 28234
Reputation: 110083
From the documentation:
>>> from django.db.models import Max
>>> AuthorizedEmail.objects.aggregate(Max('added'))
And to fetch the value in the template:
{{ item.added__max }}
Upvotes: 31
Reputation: 7349
latest
returns the latest object in the table according to the added
date:
AuthorizedEmail.objects.filter(group=group).latest('added')
Upvotes: 9
Reputation: 3488
https://docs.djangoproject.com/en/dev/topics/db/aggregation/
https://docs.djangoproject.com/en/dev/topics/db/aggregation/#cheat-sheet
Upvotes: 1
Reputation: 174624
AuthorizedEmail.objects.filter(group=group).order_by('-added')[0]
Upvotes: 5