Reputation: 757
I have a string field status
that accepts 3 values
I want to be able to sort queryset objects by the status
field by custom deciding the order :
"DELIVERED" > "ON_ITS_WAY" > "PACKAGING"
so meaning every record with the status "DELIVERED" will appear first and so on.
haven't found a way to do it with Django :(, can anyone help? thanks!
Upvotes: 1
Views: 101
Reputation: 850
let's Imagine your model is called Order
, then your queryset will look like this.
preference = Case(
When(status="DELIVERED", then=Value(0)),
When(status="ON_ITS_WAY", then=Value(1)),
When(status="PACKAGING", then=Value(2))
)
query_set = Order.objects.alias(preference=preference).order_by('preference')
Upvotes: 1