Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ORM - get() with order_by()

Tags:

python

orm

django

I want a mix of get() and order_by() like this

Model.objects.get(some=condition).order_by('name')

I want a single result (not filter) and ordered by name at the same time.

like image 820
Kelvin Barsana Avatar asked Mar 16 '26 14:03

Kelvin Barsana


2 Answers

How about

Model.objects.filter(some=condition).order_by('name').first()
like image 144
Algorithmatic Avatar answered Mar 19 '26 04:03

Algorithmatic


get() wont take order_by() as get only returns one single object and you cannot order by one one row.

and for order by you need multiple rows for the ordering. use filter('your where clause').order_by('asc/desc')

hope it helps

like image 38
Exprator Avatar answered Mar 19 '26 03:03

Exprator