Reputation: 2527
I'm currently fetching values from a queryset and serializing it into json below:
JsonResponse( list( Company.objects.defer('id').all().values() ), safe = False )
so if the company
has the following fields: Name
, Description
, Employees
How can I control the ordering of the fields. If i say order_by('Name')
then my results will be sorted according to names.
Instead, I want to specify it as Description
, Name
, Employees
and get my answer in that same order.
Upvotes: 1
Views: 61
Reputation: 1172
I'll assume that employees is a foreign key in the model company and you need to order them by 'description', 'name', 'employees name'.
It should be like this:
Company.objects.all().order_by('description', 'name', 'employees__name')
If you specify make like this:
Company.objects.all().order_by('description', 'name', 'employees')
It will sort by description, name and employees references which actually will not give the order you want.
Edit 1 If employees are character field you can do also the following
class Company(models.Model):
.....
class Meta:
ordering = ['description', 'name', 'employees']
Upvotes: 1