Skip to content

Instantly share code, notes, and snippets.

@filipgorczynski
Last active March 7, 2018 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipgorczynski/b7bf3367d832dfccff9eb9ad81ffa184 to your computer and use it in GitHub Desktop.
Save filipgorczynski/b7bf3367d832dfccff9eb9ad81ffa184 to your computer and use it in GitHub Desktop.
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