Aman Gupta
Aman Gupta

Reputation: 1874

How to get sorted queryset with multiple order by clause?

I have records in my db which are submitted by different user. I want to query such that i get some of the records but sorted in the order of time they were submitted .

I am able to achieve this using this query.

records_id_list=[5,7,9,42,55,65,78,92,116,192,785]
query_object=Records.objects.filter(record_id__in=records_id_list).order_by('registration_time').reverse()

But i want to add another order by clause where the records which is submitted by currently logged in user should appear first and then other users records(sorted only by registration_time) " submitted_by_id " is the field name and i can access the current logged in user as userId

Any help if i can achieve this in the query?

Upvotes: 0

Views: 69

Answers (1)

snejy
snejy

Reputation: 109

You can do Records.objects.filter(record_id__in=records_id_list).order_by('registration_time', 'submitted_by_id').reverse()

Here is a link to the documentation https://docs.djangoproject.com/en/1.9/ref/models/querysets/#django.db.models.query.QuerySet.order_by

Upvotes: 2

Related Questions