在django中,如何在字段上对模型进行排序然后获取最后一项?

时间:2022-06-04 07:10:25

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 object with the most recent pub_date. What is the easiest/best way to do this?

我希望能够使用最新的pub_date轻松获取对象。最简单/最好的方法是什么?

Would something like the following do what I want?

像下面这样的东西会做我想要的吗?

Edition.objects.order_by('pub_date')[:-1]

5 个解决方案

#1


33  

obj = Edition.objects.latest('pub_date')

You can also simplify things by putting get_latest_by in the model's Meta, then you'll be able to do

您还可以通过将get_latest_by放入模型的Meta中来简化操作,然后您就可以执行此操作

obj = Edition.objects.latest()

See the docs for more info. You'll probably also want to set the ordering Meta option.

有关详细信息,请参阅文档。您可能还想设置排序元选项。

#2


21  

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:

Harley的答案是你想要根据特定型号的某些订购标准获得最新情况的方法,但是一般的解决方案是颠倒订购并检索第一项:

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

#3


4  

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

普通的python列表接受负索引,它表示从列表末尾的偏移量,而不是像正数一样的开头。但是,QuerySet对象会引发

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.

#4


2  

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:

因为您可能正在索引一个空的QuerySet。我不确定正确的Pythonic方法是什么,但最简单的方法是将它包装在if / else或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.

但是,正如@Harley所说,当你按日期订购时,最新的()是这种方式的djangonic方式。

#5


0  

This has already been answered, but for more reference, this is what Django Book has to say about Slicing Data on QuerySets:

这已经得到了回答,但是为了更多的参考,这就是Django Book在QuerySet上切片数据时所说的:

Note that negative slicing is not supported:

请注意,不支持负片切片:

>>> Publisher.objects.order_by('name')[-1]
Traceback (most recent call last):
  ...
AssertionError: Negative indexing is not supported.

This is easy to get around, though. Just change the order_by() statement, like this:

不过,这很容易解决。只需更改order_by()语句,如下所示:

>>> Publisher.objects.order_by('-name')[0]

Refer the link for more such details. Hope that helps!

有关详细信息,请参阅链接。希望有所帮助!

#1


33  

obj = Edition.objects.latest('pub_date')

You can also simplify things by putting get_latest_by in the model's Meta, then you'll be able to do

您还可以通过将get_latest_by放入模型的Meta中来简化操作,然后您就可以执行此操作

obj = Edition.objects.latest()

See the docs for more info. You'll probably also want to set the ordering Meta option.

有关详细信息,请参阅文档。您可能还想设置排序元选项。

#2


21  

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:

Harley的答案是你想要根据特定型号的某些订购标准获得最新情况的方法,但是一般的解决方案是颠倒订购并检索第一项:

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

#3


4  

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

普通的python列表接受负索引,它表示从列表末尾的偏移量,而不是像正数一样的开头。但是,QuerySet对象会引发

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.

#4


2  

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:

因为您可能正在索引一个空的QuerySet。我不确定正确的Pythonic方法是什么,但最简单的方法是将它包装在if / else或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.

但是,正如@Harley所说,当你按日期订购时,最新的()是这种方式的djangonic方式。

#5


0  

This has already been answered, but for more reference, this is what Django Book has to say about Slicing Data on QuerySets:

这已经得到了回答,但是为了更多的参考,这就是Django Book在QuerySet上切片数据时所说的:

Note that negative slicing is not supported:

请注意,不支持负片切片:

>>> Publisher.objects.order_by('name')[-1]
Traceback (most recent call last):
  ...
AssertionError: Negative indexing is not supported.

This is easy to get around, though. Just change the order_by() statement, like this:

不过,这很容易解决。只需更改order_by()语句,如下所示:

>>> Publisher.objects.order_by('-name')[0]

Refer the link for more such details. Hope that helps!

有关详细信息,请参阅链接。希望有所帮助!