开发者

get_list_or_404 ordering in django

I'm trying to order the results of the get_list_or_404 method

get_list_or_404(...).order_by('name')

doesn't seem to work

What's the way to 开发者_高级运维do this?


You can do this like this:

get_list_or_404(Model.objects.order_by('name'))

And of course you can always specify ordering in Model's Meta class.


The reason your attempt didn't work is that order_by is a method on a queryset, but get_list_or_404 returns a list.

The way around this problem, as Ludwik shows in his answer, is to order the queryset before calling get_list_or_404.


In my case i have used the python's in-built reverse function to reverse the list and it worked fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜