mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-05-01 11:19:32 -05:00
Handle distinct issue
This commit is contained in:
parent
4d2e211ec1
commit
590c638ba0
@ -911,7 +911,8 @@ class DocumentsOrderingFilter(OrderingFilter):
|
|||||||
if not annotation:
|
if not annotation:
|
||||||
raise ValueError("Invalid custom field data type")
|
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
|
# We need to annotate the queryset with the custom field value
|
||||||
custom_field_value=annotation,
|
custom_field_value=annotation,
|
||||||
# We also need to annotate the queryset with a boolean for sorting whether the field exists
|
# We also need to annotate the queryset with a boolean for sorting whether the field exists
|
||||||
@ -922,13 +923,23 @@ class DocumentsOrderingFilter(OrderingFilter):
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.order_by(
|
||||||
queryset = queryset.order_by(
|
|
||||||
"-has_field",
|
"-has_field",
|
||||||
param.replace(
|
param.replace(
|
||||||
self.prefix + str(custom_field_id),
|
self.prefix + str(custom_field_id),
|
||||||
"custom_field_value",
|
"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)
|
return super().filter_queryset(request, queryset, view)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user