django模板中按日期排序错误

mad*_*ops 3 django django-templates

我得到这样的对象列表

return Post.objects.filter(categoria=categoria)
Run Code Online (Sandbox Code Playgroud)

我把它发送到模板.我在这样的模板中显示它们

{% for p in posts reversed %}
Run Code Online (Sandbox Code Playgroud)

这样我就可以获得最新的帖子了.它有99%的时间可以正常工作,但随机失败,它会显示一些较旧帖子下面的最后一篇文章.日期是正确的,最后一篇文章显示它有最新的日期,但它出现在其他一些较旧的帖子下面.

当它失败时没有什么特别的事情,我想它可能是一些不起眼的django bug.

关于可能导致这种情况的任何想法?

dcr*_*sta 14

如果您希望.order_by(...)每次查询模型时都不必使用,请使用以下ordering Meta选项:

class Post(Model):
    # your fields here
    the_date = DateTimeField(...)

    class Meta:
        # sort by "the date" in descending order unless
        # overridden in the query with order_by()
        ordering = ['-the_date']
Run Code Online (Sandbox Code Playgroud)