How to write Django queryset custom sort order like MySQL field function in Django
select status from company order by FIELD(status, 'Follow Up', 'Interested - Call back scheduled', 'Need to send details', 'Quotation sent')
Comapany.objects.all().order_by(?)
You can use objects.extra()
Company.objects.extra(
select={'manual': "FIELD(status, 'Follow Up', 'Interested - Call back scheduled', 'Need to send details', 'Quotation sent')"},
order_by=['manual']
)
Just mention the column name inside the order_by() like shown below.
Comapany.objects.all().order_by('column_name')
The above code will give the result in ascending order. If you want the result in descending order add -(minus) in front of the column name like shown below.
Comapany.objects.all().order_by('-column_name')
Hope it will help you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.