Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_list_or_404 ordering in django

Tags:

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?

like image 966
Cato Johnston Avatar asked Apr 17 '10 12:04

Cato Johnston


2 Answers

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.

like image 185
Ludwik Trammer Avatar answered Sep 29 '22 10:09

Ludwik Trammer


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.

like image 3
Alasdair Avatar answered Sep 29 '22 08:09

Alasdair