Skip to content

Commit

Permalink
feat(generic): allow apiview to choose queryset based on renderer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
b1rger committed Nov 22, 2024
1 parent 7b89774 commit 9f61378
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions apis_core/generic/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit 9f61378

Please sign in to comment.