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

Enabling all fields filtering by default #106

Open
filipetavares opened this issue May 13, 2021 · 0 comments
Open

Enabling all fields filtering by default #106

filipetavares opened this issue May 13, 2021 · 0 comments

Comments

@filipetavares
Copy link

I'm opening this issue to ask if this if statement is required, since if you remove it you will enable all fields to be filtered upon as long as you define the DEFAULT_BACKEND_FILTER in your settings.py.

if filter_fields:
model = filter_class_default.filter_backend_class(queryset).get_model()
meta_kwargs = filter_class_meta_kwargs.copy()
meta_kwargs.update({"model": model, "fields": filter_fields})
meta = type(str("Meta"), (object,), meta_kwargs)
return type(
str("{}FilterSet".format(model.__name__)),
(filter_class_default,),
{"Meta": meta},
)

From what I've read on the docs, this should be the default behaviour, but it doesn't work on GenericAPIViews, because if I don't define some fieldset_fields on the view it won't generate a filterset class since the condition above will fail.

For context, this is my view:

class UsersView(GenericAPIView):

    queryset = User.objects.all()
    serializer_class = UserSerializer
    permission_classes = [IsAuthenticated]


    def get_queryset(self):
        return self.filter_queryset(super().get_queryset())

    def get(self, request):

        serializer = self.get_serializer(self.get_queryset(), many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK )

and these are my settings:

REST_FRAMEWORK = {
  ...
    'DEFAULT_FILTER_BACKENDS': [
        'url_filter.integrations.drf.DjangoFilterBackend',
    ]
}

By default is doesn't filter by any field, but if I remove only the if statement (line 93) it works fine.

So am I setting something wrong, or is this an actual bug?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant