34👍
✅
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.
3👍
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
.
- Django-templates: Why doesn't {% if "string"|length > 10 %} work at all?
- Django.core.exceptions.FieldDoesNotExist: model has no field named <function SET_NULL at 0x7fc5ae8836e0>
0👍
In my case i have used the python’s in-built reverse function to reverse the list and it worked fine.
- Colorizing the output of Django tests
- Is it correct to modify old migration files in Django?
- Getting a list from Django into Javascript as an array
- No Such Column Error in Django App After South Migration
Source:stackexchange.com