From 9f61378aec50940fe94da6fe0252f050dfca121f Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Fri, 22 Nov 2024 09:02:48 +0100 Subject: [PATCH] feat(generic): allow apiview to choose queryset based on renderer Add additional options to the list of possible querysets, based on the renderer chosen. This allows users to define separate querysets for separate renderers. Closes: #1307 --- apis_core/generic/api_views.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apis_core/generic/api_views.py b/apis_core/generic/api_views.py index 1e70ad1ee..4ed79e483 100644 --- a/apis_core/generic/api_views.py +++ b/apis_core/generic/api_views.py @@ -21,9 +21,19 @@ def dispatch(self, *args, **kwargs): return super().dispatch(*args, **kwargs) def get_queryset(self): + renderer = getattr(getattr(self, "request", {}), "accepted_renderer", None) queryset_methods = module_paths( self.model, path="querysets", suffix="ViewSetQueryset" ) + if renderer is not None: + prefix = makeclassprefix(renderer.format) + queryset_methods = ( + module_paths( + self.model, path="querysets", suffix=f"{prefix}ViewSetQueryset" + ) + + queryset_methods + ) + queryset = first_member_match(queryset_methods) or (lambda x: x) return queryset(self.model.objects.all())