Django Rest Filter
Project description
dj-rest-filters
dj-rest-filters is a Django application allowing users to declaratively add dynamic QuerySet filtering from URL parameters. Its uses Django Rest Framework serializers in backend so it provides same syntax as of serializers. You can validate you query parameter in same way as you validate in your serializer and it also provides filtering mechanism against custom query parameters.
Installation
pip install dj-rest-filters
Then add 'djfilters' to your INSTALLED_APPS.
Usage
Filtering
from rest_framework import generics
from djfilters.backend import DjFilterBackend
class TodoView(generics.GenericAPIView):
serializer_class = ...
filter_class = TodoFilter
filter_backends = [DjFilterBackend]
queryset = Todo.objects.all()
Validation Only
When queryset is declared, filter will be used to filter that queryset. But when there is not any queryset, filter will just be used to validate the provided query parameters.
from rest_framework import generics
class TodoView(generics.GenericAPIView):
serializer_class = ...
filter_class = MyFilter
Filter Context
Just like serializer context, context can be passed to filter by using get_filter_context
function like this.
from rest_framework import generics
class TodoView(generics.GenericAPIView):
....
def get_filter_context(self):
context = {'extra_data': 'some_extra_data'}
return context
Accessing Validated Query Params
After query param validation, validated parameters can be accessed using request.cleaned_args
.
For detailed documentation, click here.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dj-rest-filters-1.0.4.tar.gz
.
File metadata
- Download URL: dj-rest-filters-1.0.4.tar.gz
- Upload date:
- Size: 21.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef1f3dc1355313bf3046106e1cac633f10e27454e065f5d2dde477bde74a5a6c |
|
MD5 | 8139642f3f0d4bd3342207f35d72bad7 |
|
BLAKE2b-256 | 1d955610f7eaf844bd177ffac641bf51ac30093e26ff9a866efd87969fe329e2 |
File details
Details for the file dj_rest_filters-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: dj_rest_filters-1.0.4-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05eb817d159d1207b6ed2d780d1e5b28d9bb9a2410bea0f721b0601849ef53c9 |
|
MD5 | 127f8a1d130bc7bfef2aac3a46228091 |
|
BLAKE2b-256 | 30f2dfc5ab0dfc5924658ad380566db6ee0f7322194ef99472e8af7ab8b83d43 |