Skip to content

Commit 5508e12

Browse files
committed
Fixes #14512: Omit unused queryset annotations for REST API requests using brief mode
1 parent 69bf147 commit 5508e12

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

netbox/netbox/api/viewsets/mixins.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,15 @@ def get_serializer_class(self):
5656
def get_queryset(self):
5757
qs = super().get_queryset()
5858

59-
# If using brief mode, clear all prefetches from the queryset and append only brief_prefetch_fields (if any)
6059
if self.brief:
60+
serializer_class = self.get_serializer_class()
61+
62+
# Clear any annotations for fields not present on the nested serializer
63+
for annotation in list(qs.query.annotations.keys()):
64+
if annotation not in serializer_class().fields:
65+
qs.query.annotations.pop(annotation)
66+
67+
# Clear any prefetches from the queryset and append only brief_prefetch_fields (if any)
6168
return qs.prefetch_related(None).prefetch_related(*self.brief_prefetch_fields)
6269

6370
return qs

0 commit comments

Comments
 (0)