From 4d2e211ec10fc23f5c660bb7542eb2443210e7bb Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 11 Dec 2024 13:42:00 -0800 Subject: [PATCH] Use an exists subquery --- src/documents/filters.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/documents/filters.py b/src/documents/filters.py index ee9d666c3..7cf2211e0 100644 --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -10,6 +10,7 @@ from django.db.models import Case from django.db.models import CharField from django.db.models import Count from django.db.models import DateTimeField +from django.db.models import Exists from django.db.models import FloatField from django.db.models import IntegerField from django.db.models import OuterRef @@ -914,13 +915,11 @@ class DocumentsOrderingFilter(OrderingFilter): # 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 - has_field=Case( - When( - custom_fields__field_id=custom_field_id, - then=Value(1), + has_field=Exists( + CustomFieldInstance.objects.filter( + document_id=OuterRef("id"), + field_id=custom_field_id, ), - default=Value(0), - output_field=IntegerField(), ), )