Django Rest Filter
Project description
dj-rest-filters
A Django REST Framework filter backend that lets you declaratively validate query parameters and filter querysets using DRF serializer syntax. Validate query params the same way you validate request bodies, with the same fields, the same Meta, and the same validate_* hooks.
📖 Full docs: https://dj-rest-filters.readthedocs.io/en/latest/
Features
- DRF-native syntax — filters look like serializers; reuse what you already know.
- Validation-only mode — drop the queryset to use a filter purely for query-param validation; cleaned values land on
request.cleaned_args. ListFieldandRangeFieldforINand range queries; accepts comma-separated, JSON-array, or repeated-key inputs.- Custom per-field filtering via
filter_<name>(self, qs, value), pluslookup_expr,exclude=True, anddistinct=Trueon every field. - OpenAPI / Swagger — schema parameters auto-generated for both drf-spectacular and drf-yasg.
- Browsable API support out of the box.
Requirements
| Minimum | Tested up to | |
|---|---|---|
| Python | 3.8 | 3.14 |
| Django | 3.2 | 5.2 |
| Django REST Framework | 3.12 | 3.17 |
Installation
pip install dj-rest-filters
Add djfilters to your INSTALLED_APPS:
INSTALLED_APPS = [
...,
"djfilters",
]
Quickstart
Define a filter — same shape as a DRF serializer:
from djfilters import filters
class TodoFilter(filters.Filter):
title = filters.CharField(required=False, lookup_expr="icontains")
completed = filters.BooleanField(required=False)
tags = filters.ListField(child=filters.CharField(), required=False)
Wire it into a view:
from rest_framework import generics
from djfilters.backend import DjFilterBackend
class TodoView(generics.ListAPIView):
queryset = Todo.objects.all()
serializer_class = TodoSerializer
filter_class = TodoFilter
filter_backends = [DjFilterBackend]
GET /todos/?title=buy&completed=true&tags=urgent,today filters by title__icontains="buy", completed=True, and tags__in=("urgent", "today").
Validation-only mode
Omit queryset to use a filter purely for query-param validation. The filter still runs, errors still surface as 400s, and the cleaned values land on request.cleaned_args:
class TodoView(generics.GenericAPIView):
serializer_class = TodoSerializer
filter_class = TodoFilter
filter_backends = [DjFilterBackend]
def get(self, request):
params = request.cleaned_args # validated query params
...
Filter context
Just like serializer context, extra context can be passed to the filter from the view:
class TodoView(generics.ListAPIView):
...
def get_filter_context(self):
return {"user": self.request.user}
The returned dict is merged with the default {"request": ...} and is accessible as self.context inside the filter.
Documentation
License
MIT — see LICENSE.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_rest_filters-1.1.0.tar.gz.
File metadata
- Download URL: dj_rest_filters-1.1.0.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcb911bc5ebe8731aeca1199ba2503977172b3c79656f20808f6f2cfccabbfd4
|
|
| MD5 |
f14bf3172544019c08049a4cce187dbc
|
|
| BLAKE2b-256 |
3943ccd80f8875452bb47f761053706b5c5566d9f4b8d15344614deb59dd7f82
|
Provenance
The following attestation bundles were made for dj_rest_filters-1.1.0.tar.gz:
Publisher:
release.yml on mhsiddiqui/dj-rest-filters
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_rest_filters-1.1.0.tar.gz -
Subject digest:
dcb911bc5ebe8731aeca1199ba2503977172b3c79656f20808f6f2cfccabbfd4 - Sigstore transparency entry: 1397662130
- Sigstore integration time:
-
Permalink:
mhsiddiqui/dj-rest-filters@9d9fd3297c9699780a706d6243c4aa36974c33e4 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/mhsiddiqui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9d9fd3297c9699780a706d6243c4aa36974c33e4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dj_rest_filters-1.1.0-py3-none-any.whl.
File metadata
- Download URL: dj_rest_filters-1.1.0-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1d51e762a4dc0d6e0dc4885b49bb9a946ea4704f6cafa0071c5c2482cd7f9a6
|
|
| MD5 |
1715c76b220062c638d446f21c94a74f
|
|
| BLAKE2b-256 |
7826f23dab46193de6217be5cc15c10ddbc81b3c2e63a261a9f915a8eb0a1ed0
|
Provenance
The following attestation bundles were made for dj_rest_filters-1.1.0-py3-none-any.whl:
Publisher:
release.yml on mhsiddiqui/dj-rest-filters
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_rest_filters-1.1.0-py3-none-any.whl -
Subject digest:
e1d51e762a4dc0d6e0dc4885b49bb9a946ea4704f6cafa0071c5c2482cd7f9a6 - Sigstore transparency entry: 1397662147
- Sigstore integration time:
-
Permalink:
mhsiddiqui/dj-rest-filters@9d9fd3297c9699780a706d6243c4aa36974c33e4 -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/mhsiddiqui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9d9fd3297c9699780a706d6243c4aa36974c33e4 -
Trigger Event:
release
-
Statement type: