Handle distinct issue

This commit is contained in:
shamoon 2024-12-11 14:14:42 -08:00
parent 4d2e211ec1
commit 590c638ba0

View File

@ -911,7 +911,8 @@ class DocumentsOrderingFilter(OrderingFilter):
if not annotation:
raise ValueError("Invalid custom field data type")
queryset = queryset.annotate(
ids_sorted_by_cf = (
queryset.annotate(
# We need to annotate the queryset with the custom field value
custom_field_value=annotation,
# We also need to annotate the queryset with a boolean for sorting whether the field exists
@ -922,13 +923,23 @@ class DocumentsOrderingFilter(OrderingFilter):
),
),
)
queryset = queryset.order_by(
.order_by(
"-has_field",
param.replace(
self.prefix + str(custom_field_id),
"custom_field_value",
),
)
.values_list("id", flat=True)
)
# We need to preserve the order of the ids sorted by custom field, see https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct
preserved = Case(
*[
When(id=id, then=position)
for position, id in enumerate(ids_sorted_by_cf)
],
)
queryset = queryset.filter(id__in=ids_sorted_by_cf).order_by(preserved)
return super().filter_queryset(request, queryset, view)