[Answered ]-Order strings fields in django QuerySet by specific order

1👍

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')

Check these methods from Official Django Docs:

When

Case

alias

Leave a comment