Skip to main content

Django Rest Filter

Project description

dj-rest-filters

Build codecov Docs

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.
  • ListField and RangeField for IN and range queries; accepts comma-separated, JSON-array, or repeated-key inputs.
  • Custom per-field filtering via filter_<name>(self, qs, value), plus lookup_expr, exclude=True, and distinct=True on 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

dj_rest_filters-1.1.0.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_rest_filters-1.1.0-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

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

Hashes for dj_rest_filters-1.1.0.tar.gz
Algorithm Hash digest
SHA256 dcb911bc5ebe8731aeca1199ba2503977172b3c79656f20808f6f2cfccabbfd4
MD5 f14bf3172544019c08049a4cce187dbc
BLAKE2b-256 3943ccd80f8875452bb47f761053706b5c5566d9f4b8d15344614deb59dd7f82

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_rest_filters-1.1.0.tar.gz:

Publisher: release.yml on mhsiddiqui/dj-rest-filters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for dj_rest_filters-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1d51e762a4dc0d6e0dc4885b49bb9a946ea4704f6cafa0071c5c2482cd7f9a6
MD5 1715c76b220062c638d446f21c94a74f
BLAKE2b-256 7826f23dab46193de6217be5cc15c10ddbc81b3c2e63a261a9f915a8eb0a1ed0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page