philipn/django-rest-framework-filters

Trying out ComplexJSONFilterBackend

lofidevops opened this issue · 1 comments

@ferrants I'm trying out your json-filter branch, but although I'm submitting valid JSON, I can't seem to trigger a search. Would you mind looking at what I'm doing wrong? Here's a simple REST view for users:

class UserList(generics.ListCreateAPIView):
    queryset = get_user_model().objects.all().order_by("username")
    serializer_class = UserSerializer
    permission_classes = (UserRolePermissions,)
    filter_backends = (filters.backends.ComplexJSONFilterBackend,)

and a trivial query, which I dump to json and encode with urllib.parse.quote:

{"or": [{"username__startswith": "a"},{"username__startswith": "b"}]}

But when I append this to URL/?json_filters= it has no effect. The JSON filter is active, because if I append URL/?json_filters=ksjfhshfjdsgfe I get a JSON parsing error.

Similarly, if I use this query I would expect to get no results, but instead I still get all results:

{"and": [{"username__eq": "x"},{"username__eq": "y"}]}

Similarly, if I enter valid JSON with nonsense content, I get all results:

{"and": [{"foo": "bar"},{"spam": "eggs"}]}

Finally, if I enter an empty dictionary, I get all results, but I think that's expected (no filter == all results):

{}

Is there a section of the branch documentation, django-rest-framework-filters documentation or django-filters documentation that I've overlooked?

@david-libremone , it looks like you're using the API right. What I suspect is happening is that you haven't defined a filterset_class or filter_class (deprecated) on the view. This will run all of those "leaf" nodes through that class as if those are the only parameters, produce querysets for each and then combine them based on the boolean logic you specify.