From 69cbd76471bd89fcf843c1994e6519aada7fa1f3 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 14 Dec 2023 13:33:11 -0500 Subject: [PATCH] Fixes #14512: Omit unused queryset annotations for REST API requests using brief mode --- netbox/netbox/api/viewsets/mixins.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/netbox/netbox/api/viewsets/mixins.py b/netbox/netbox/api/viewsets/mixins.py index fde486fe967..315563e1a5c 100644 --- a/netbox/netbox/api/viewsets/mixins.py +++ b/netbox/netbox/api/viewsets/mixins.py @@ -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