inspired by this SO question… Imagine you want force Django to some custome ordering. And you wish to do it on big queryset, and don’t want nasty hacks with lambdas and lists in Python, but want use the power of your database. The database sudocode could look something like this: order by


    when id = 5 then 1

    when id = 2 then 2

    when id = 3 then 3

    when id = 1 then 4

    when id = 4 then 5

end) asc

Example solution using django 1.8 conditional expressions (Case, When) may be:

from django.db.models import Case, When, Value, IntegerField

        When(id=5, then=Value(1)),
        When(id=2, then=Value(2)),
        When(id=3, then=Value(3)),
        When(id=1, then=Value(4)),
        When(id=4, then=Value(5)),

More about conditional expressions in Django: