DRF Auto Filters
Overview
DRF Auto Filters automatically generates and Integrate your swagger docs UI intelligent filter sets for Django REST Framework based on your model field types eliminating the need to manually create filter classes for each model.
Features
- Automatic Filter Generation: Creates appropriate filters for each model field type without any manual configuration
- Field-Type Specific Filters:
- Text fields: exact, case-insensitive, contains, starts with, ends with
- Numeric fields: exact, min, max
- Date/DateTime fields: exact, before, after
- Boolean fields: true/false
- Foreign keys: ID-based filtering
- Many-to-Many relationships: contains filtering
- DRF Integration: Seamlessly works with Django REST Framework's filter backend system
- Swagger Documentation: Automatic API documentation with drf-yasg integration
- Customizable: Easily extendable to add custom filters or modify existing ones
Installation
pip install drf-auto-filters
Add to your INSTALLED_APPS:
INSTALLED_APPS = [
# ...
'rest_framework',
'django_filters',
'drf_auto_filters',
# ...
]
Quick Start
Simply add the AutoFilterBackend to your viewset:
from rest_framework import viewsets
from drf_auto_filters import AutoFilterBackend
from .models import Book
from .serializers import BookSerializer
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [AutoFilterBackend]
That's it! Your API now supports a wide range of filters automatically and a Swagger UI Integration (drf_yasg)
Filter Examples
Given a model like:
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
publication_date = models.DateField()
price = models.DecimalField(max_digits=6, decimal_places=2)
is_bestseller = models.BooleanField(default=False)
genres = models.ManyToManyField(Genre)
Your API will automatically support these filter queries:
# Text filters
/api/books/?title_exact=Django Rest Framework
/api/books/?title_iexact=django rest framework
/api/books/?title_contains=Django
/api/books/?title_partial=django
/api/books/?title_startswith=dj
/api/books/?title_endswith=framework
# Numeric filters
/api/books/?price_exact=19.99
/api/books/?price_min=15.00
/api/books/?price_max=25.00
# Date filters
/api/books/?publication_date_exact=2020-01-01
/api/books/?publication_date_after=2020-01-01
/api/books/?publication_date_before=2020-12-31
# Boolean filters
/api/books/?is_bestseller=true
# Relationship filters
/api/books/?author_id=1
/api/books/?genres_contains=1,2,3
Limiting Fields
If you want to limit which fields get filters, add a filter_set_fields attribute to your view:
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [AutoFilterBackend]
filter_set_fields = ['title', 'price', 'publication_date']
Custom Swagger Integration
DRF Auto Filters integrates with drf-yasg to automatically document your filters:
from drf_auto_filters import swagger_auto_schema_with_filters
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
filter_backends = [AutoFilterBackend]
@swagger_auto_schema_with_filters()
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
Documentation
For more detailed documentation, see:
Requirements
- Python (3.8, 3.9, 3.10, 3.11)
- Django (3.2, 4.0, 4.1, 4.2)
- Django REST Framework (3.12+)
- django-filter (22.1+)
- drf-yasg (1.20+) (optional, for Swagger integration)
Running Tests
pip install tox
tox
License
MIT License - see the LICENSE file for details.
Metadata
Release files for drf-auto-filters 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_auto_filters-0.1.0.tar.gz | 16.3 kB | Without provenance | Details for drf_auto_filters-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_auto_filters-0.1.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for drf_auto_filters-0.1.0-py2.py3-none-any.whl |
Total release size: 25.1 kB
Release files / drf_auto_filters-0.1.0.tar.gz
| Download URL | drf_auto_filters-0.1.0.tar.gz |
|---|---|
| Size | 16.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f5e4fb589598c59772804e4380e28a5ef18923ea2a329eecfd84288b9be916b
|
|
BLAKE2b-256 checksum How to use checksums |
0de7a70766f589ea090903d06c9e1cd0cc4629f265d0f01f36fdf6c770f8af38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Release files / drf_auto_filters-0.1.0-py2.py3-none-any.whl
| Download URL | drf_auto_filters-0.1.0-py2.py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ef47e1fd14d075945a745ae84add1dfe9bcd88d4d6eff96a7cc9e56dc0428e66
|
|
BLAKE2b-256 checksum How to use checksums |
9d683196efe72fe27ca3ccc5412754580c6c9c90c7d098e6653ff49036602237
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.8.10
|
Security for drf-auto-filters 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.