Get objects, but have one object always as the first result?

Is it possible to have a QuerySet that retrieves a list of objects, but returns a specific object always first? I need it as a queryset, so I can pass it to the queryset property of a form.ModelChoiceField.

This is the only approach that I could think of, but it doesn’t work.

favourite = 12
query = Books.objects.filter(pk=favourite) | Books.objects.all().exclude(pk=favourite)

You can use QuerySet.order_by for this purpose. Something along these lines should work

from django.db.models.lookups import Exact

    Exact(F("pk"), favourite)).desc()

Which should result in the following SQL

FROM book
ORDER BY (pk = 12) DESC
1 Like