Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Order_by multiple fields

I am getting order_by fields in the form of a list. I want to order_by by multiple fields with django orm. List is like below:

orderbyList = ['check-in','check-out','location'] 

I am writing a query is like:

modelclassinstance.objects.all().order_by(*orderbyList) 

Everything i am expecting in a list is dynamic. I don't have predifined set of data.Could some tell me how to write a django ORM with this?

like image 280
danny Avatar asked Apr 12 '16 05:04

danny


1 Answers

Try something like this

modelclassinstance.objects.order_by('check-in', 'check-out', 'location') 

You don't need .all() for this

You can also define ordering in your model class

something like

class Meta:        ordering = ['check-in', 'check-out', 'location'] 
like image 75
burning Avatar answered Sep 22 '22 05:09

burning