care.utils.queryset.filters module

sort_index(field_name: str, order: list[Any] | None) Case

Creates a Case expression that can be used to sort a queryset based on a specified order list.

This function is useful for ordering QuerySet results to match a predefined sequence. For example, to display certain items first in a specific order.

Parameters:
  • field_name – The name of the model field to compare against values in the order list.

  • order – A list of values to be used for ordering. Records with field values matching items in this list will be ordered according to their position in the list. If None or empty, returns an empty Case that won’t affect ordering.

Returns:

A Django Case expression that can be used in QuerySet.annotate() or order_by().

Examples

# Sort patients by priority status: [‘critical’, ‘urgent’, ‘stable’] patients = Patient.objects.annotate(

priority_order=sort_index(‘status’, [‘critical’, ‘urgent’, ‘stable’])

).order_by(‘priority_order’)