Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support views with no model #164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ path("api/", include([
])),
```

### Handling Model-less Querysets
Worf now supports handling custom querysets without relying on a model. If you need to provide your own queryset or response without using a model, you can set the model attribute to None or False. The AnnotatedModelFilterSet and generate_filterset function have been updated to handle these cases properly.

To use this feature, you should make sure your view class returns a queryset by overriding the get_queryset() method or returning a response via the get() method.

Serializers
-----------

Expand Down
10 changes: 10 additions & 0 deletions worf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class AnnotatedModelFilterSet(ModelFilterSet):
def get_filters(self):
if self.Meta.model is None or self.Meta.model is False:
return {}

filters = super().get_filters()

if self.queryset is not None:
Expand Down Expand Up @@ -44,6 +47,13 @@ def _build_annotation_filter(self, name, state):


def generate_filterset(model, queryset):
if model is None or model is False:
return type(
f"EmptyFilterSet",
(AnnotatedModelFilterSet,),
dict(Meta=type("Meta", (), dict(model=None, queryset=queryset))),
)

return type(
f"{model.__name__}FilterSet",
(AnnotatedModelFilterSet,),
Expand Down