Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
queryset = queryset.annotate(
exact=Case(
When(
name__iexact=query,
then=Value('1')
),
When(
name__istartswith=query,
then=Value('2')
),
When(
name__iendswith=query,
then=Value('3')
),
When(
name__icontains=query,
then=Value('4')
),
default=Value('5'),
output_field=CharField(),
)
).order_by('exact', 'name')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.