Skip to content

Commit

Permalink
Fixes #14512: Omit unused queryset annotations for REST API requests …
Browse files Browse the repository at this point in the history
…using brief mode
  • Loading branch information
jeremystretch committed Dec 14, 2023
1 parent b937358 commit 69cbd76
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion netbox/netbox/api/viewsets/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,15 @@ def get_serializer_class(self):
def get_queryset(self):
qs = super().get_queryset()

# If using brief mode, clear all prefetches from the queryset and append only brief_prefetch_fields (if any)
if self.brief:
serializer_class = self.get_serializer_class()

# Clear any annotations for fields not present on the nested serializer
for annotation in list(qs.query.annotations.keys()):
if annotation not in serializer_class().fields:
qs.query.annotations.pop(annotation)

# Clear any prefetches from the queryset and append only brief_prefetch_fields (if any)
return qs.prefetch_related(None).prefetch_related(*self.brief_prefetch_fields)

return qs
Expand Down

0 comments on commit 69cbd76

Please sign in to comment.