开发者

wrong order by date in django template

I get a list of objects like this

return Post.objects.filter(categoria=categoria)

And I send that to a template. I display them in the template like this

{% for p in posts reversed %}

That way I get the new posts on top. It works like that 99% of the time but it fails randomly were it will show the last post below some older post. The dates are correct, the last post shows that it has the most recent date yet it appears below some other older post.

Nothing special is done when it fails, I'm thinking开发者_Python百科 it might be some obscure django bug.

Any ideas about what could be causing this?


If you want to avoid having to use .order_by(...) each time you query your model, use the ordering Meta option:

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']


Including an order_by when getting the objects seem to fix the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜