Skip to content

Conversation

abdulhaq-e
Copy link
Contributor

Added filter feature. This uses DjangoFilterBackend which either
comes from rest-framework-filter or django-filter. The former
is a package which adds more features to the latter.

All that is done is reformatting the request query parameters to be
compatible with DjangoFilterBackend. The spec recommend the pattern
filter[field]=values. JsonApiFilterBackend takes that pattern and
reformats it to become field=value which will then work as typical
filtering in DRF.

The specs only rule towards filtering is that the filter keyword
should be reserved. It doesn't however say about the exact format.
It only recommends filter[field]=value but this can also be
filter{field}=value or filter(field)=value. Therefore, a new
setting for DJA is introduced: JSON_API_FILTER_KEYWORD. This is a
regex which controls the filter format. Its default value is:

JSON_API_FILTER_KEYWORD = 'filter\[(?P<field>\w+)\]'

It can be changed to anything as long as the field keyword is
included in the regex.

The docs have been updated.

Unfortunately, the editor's beautifier was run and made lots of
other changes to the code style. No harm was done though, that's
why TDD exists :D

@abdulhaq-e abdulhaq-e force-pushed the filter_feature branch 2 times, most recently from 54cabcd to 9ce5035 Compare October 7, 2016 21:22
@codecov-io
Copy link

codecov-io commented Oct 7, 2016

Current coverage is 91.39% (diff: 94.18%)

Merging #286 into develop will increase coverage by 0.24%

@@            develop       #286   diff @@
==========================================
  Files            49         52     +3   
  Lines          2295       2372    +77   
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits           2092       2168    +76   
- Misses          203        204     +1   
  Partials          0          0          

Powered by Codecov. Last update 1623942...a51f8e2

Copy link

@khornberg khornberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this allow filtering on multiple fields at the same time?

Does this allow filtering on multiple values of the same field?

Does this allow filtering of array, hstore and other complex/DB specific fields?

def filter_queryset(self, request, queryset, view):

filter_class = self.get_filter_class(view, queryset)
print(request.query_params)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return resource_name

# the name was calculated automatically from the view > pluralize and format
# the name was calculated automatically from the view > pluralize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing all these things is not helpful, even if your dev software did it for you. It only adds noise to the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. However, this project in general needs linting clean up. If the owners agree, we'll agree on a format and clean up the code. I'll also delete these linting changes if required.

from example.models import Comment


class CommentFilter(django_filters.FilterSet):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There does not seem to be an example of using DRF filters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide a link that shows an example?

DRF filtering depends on django_filter and similar examples are found in DRF docs.

Added filter feature. This uses DjangoFilterBackend which either
comes from `rest-framework-filter` or `django-filter`. The former
is a package which adds more features to the latter.

All that is done is reformatting the request query parameters to be
compatible with DjangoFilterBackend. The spec recommend the pattern
`filter[field]=values`. JsonApiFilterBackend takes that pattern and
reformats it to become `field=value` which will then work as typical
filtering in DRF.

The specs only rule towards filtering is that the `filter` keyword
should be reserved. It doesn't however say about the exact format.
It only **recommends** `filter[field]=value` but this can also be
`filter{field}=value` or `filter(field)=value`. Therefore, a new
setting for DJA is introduced: JSON_API_FILTER_KEYWORD. This is a
regex which controls the filter format. Its default value is:

```
JSON_API_FILTER_KEYWORD = 'filter\[(?P<field>\w+)\]'
```

It can be changed to anything as long as the `field` keyword is
included in the regex.

The docs have been updated.

Unfortunately, the editor's beautifier was run and made lots of
other changes to the code style. No harm was done though, that's
why TDD exists :D
@abdulhaq-e
Copy link
Contributor Author

Does this allow filtering on multiple fields at the same time?

Yes, it is permitted to send a request such as:

GET http://localhost/?filter[author]=1&filter[blog]=10

Does this allow filtering on multiple values of the same field?
Does this allow filtering of array, hstore and other complex/DB specific fields?

The JSON API specs does not get into any of this, it depends on the user implementation. However, this is all possible using django_filter.

@abdulhaq-e abdulhaq-e closed this Feb 3, 2017
@abdulhaq-e abdulhaq-e deleted the filter_feature branch February 3, 2017 06:58
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

Successfully merging this pull request may close these issues.

3 participants