Skip to main content

Automatic filter generation for Django Rest Framework based on model field types

Project description

DRF Auto Filters

PyPI version Tests codecov Python Versions Django Versions

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.

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

drf_auto_filters-0.1.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

drf_auto_filters-0.1.0-py2.py3-none-any.whl (8.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file drf_auto_filters-0.1.0.tar.gz.

File metadata

  • Download URL: drf_auto_filters-0.1.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for drf_auto_filters-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f5e4fb589598c59772804e4380e28a5ef18923ea2a329eecfd84288b9be916b
MD5 4033a12086abc08bc26a3bc0922940c7
BLAKE2b-256 0de7a70766f589ea090903d06c9e1cd0cc4629f265d0f01f36fdf6c770f8af38

See more details on using hashes here.

File details

Details for the file drf_auto_filters-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for drf_auto_filters-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ef47e1fd14d075945a745ae84add1dfe9bcd88d4d6eff96a7cc9e56dc0428e66
MD5 02daa25583e31426fa938a8b8fb4a875
BLAKE2b-256 9d683196efe72fe27ca3ccc5412754580c6c9c90c7d098e6653ff49036602237

See more details on using hashes here.

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