Complex order in django db request

I have the following entries in my database:

`title` `status` Titanic WIP Avatar WIP Terminator Complete Abyss Default 

I want to sort these objects by object status: by default, then by WIP, and then upon completion. Then the correct order is:

 Abyss / Default Avatar / WIP Titanic / WIP Terminator / Complete 

How do I make the next db call?

 Title.objects.order_by(status='Default', status='WIP', status='Complete',title) 
+4
source share
1 answer

To make this request, you can use django extra :

 titles = Title.objects.all() ordered_query = titles.extra(select={ 'ordering':"( case when status='Default' then 1 when status='WIP' then 2 when status='Complete' then 3 end)" }).order_by('ordering', 'title') 
+3
source

All Articles