[Answered ]-Django queryset order_by specify order

1👍

You can work with a condition expression [Django-doc]:

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

Project.objects.alias(
    color_order=Case(
        *[When(color=color, then=Value(i)) for i, color in enumerate(COLORS)],
        output_field=IntegerField()
    )
).order_by('color_order')

Leave a comment