From 240d22696f4840b7f7a2143d7ec28016e2188f40 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Tue, 20 Nov 2018 21:02:06 -0500 Subject: [PATCH] Strip annotations from queryset when retrieving object count for API pagination --- netbox/netbox/api.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/netbox/netbox/api.py b/netbox/netbox/api.py index cc4f7a1aa..743d890fe 100644 --- a/netbox/netbox/api.py +++ b/netbox/netbox/api.py @@ -81,10 +81,17 @@ class OptionalLimitOffsetPagination(LimitOffsetPagination): def paginate_queryset(self, queryset, request, view=None): - try: - self.count = queryset.count() - except (AttributeError, TypeError): + if hasattr(queryset, 'all'): + # TODO: This breaks filtering by annotated values + # Make a clone of the queryset with any annotations stripped (performance hack) + qs = queryset.all() + qs.query.annotations.clear() + self.count = qs.count() + + else: + # We're dealing with an iterable, not a QuerySet self.count = len(queryset) + self.limit = self.get_limit(request) self.offset = self.get_offset(request) self.request = request