Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 it might be some obscure django bug.

Any ideas about what could be causing this?

like image 506
madprops Avatar asked Mar 21 '11 01:03

madprops


1 Answers

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']
like image 117
dcrosta Avatar answered Oct 07 '22 05:10

dcrosta