154👍
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']
10👍
Pass orders list in query parameters
eg : yourdomain/?order=location&order=check-out
default_order = ['check-in'] #default order
order = request.GET.getlist('order', default_order)
modelclassinstance.objects.all().order_by(*orderbyList)
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Django edit user profile
- [Django]-Programmatically saving image to Django ImageField
1👍
modelclassinstance.objects.filter(anyfield=’value’).order_by(‘check-in’, ‘check-out’, ‘location’)
- [Django]-Using Django auth UserAdmin for a custom user model
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-Filtering dropdown values in django admin
0👍
You should pass a list of stringified arguments of the database table columns (as specified by your ORM in the respective models.py file) to the .order_by()
method on the return query set like so. shifts = Shift.objects.order_by('start_time', 'employee_first_name')
. By notation,.order_by(**args)
, will help you remember that takes an arbitrary number of arguments.
- [Django]-Django, creating a custom 500/404 error page
- [Django]-How to add url parameters to Django template url tag?
- [Django]-Django: why i can't get the tracebacks (in case of error) when i run LiveServerTestCase tests?
-3👍
Try this:
listOfInstance = modelclassinstance.objects.all()
for askedOrder in orderbyList:
listOfInstance = listOfInstance.order_by(askedOrder)
- [Django]-Django logging of custom management commands
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Django: guidelines for speeding up template rendering performance
-6👍
What you have to do is chain the querySets, in other words:
classExample.objects.all().order_by(field1).order_by(field2)...
- [Django]-How to change field name in Django REST Framework
- [Django]-VueJS + Django Channels
- [Django]-IOS app with Django
Source:stackexchange.com