Specifically, I have a model that has a field like this
pub_date = models.DateField(\"date published\")
I want to be able to easily grab the ob
Be careful of using
Edition.objects.order_by('-pub_date')[0]
as you might be indexing an empty QuerySet. I'm not sure what the correct Pythonic approach is, but the simplest would be to wrap it in an if/else or try/catch:
try:
last = Edition.objects.order_by('-pub_date')[0]
except IndexError:
# Didn't find anything...
But, as @Harley said, when you're ordering by date, latest()
is the djangonic way to do it.