In django, how do I sort a model on a field and then get the last item?

后端 未结 5 2039
青春惊慌失措
青春惊慌失措 2021-02-12 11:20

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

5条回答
  •  感动是毒
    2021-02-12 11:51

    Harley's answer is the way to go for the case where you want the latest according to some ordering criteria for particular Models, as you do, but the general solution is to reverse the ordering and retrieve the first item:

    Edition.objects.order_by('-pub_date')[0]
    

提交回复
热议问题