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

后端 未结 5 2042
青春惊慌失措
青春惊慌失措 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 12:04

    Note:

    Normal python lists accept negative indexes, which signify an offset from the end of the list, rather than the beginning like a positive number. However, QuerySet objects will raise

    AssertionError: Negative indexing is not supported.
    if you use a negative index, which is why you have to do what insin said: reverse the ordering and grab the 0th element.

提交回复
热议问题