CodgeBush

CodgeBush is my personal library for hand-picked code snippets to get the work done in plenty of minutes

View the Project on GitHub thewolfcommander/CodgeBush

Django Filters Integration with DRF

In this module I’ve integrated django-filter module with Django REST Framework

Configuration


"""
STEP 1:
Installation

pip install django-filter
"""

"""
Step 2:
Configuring settings.py
"""

INSTALLED_APPS = [
    ...
    'rest_framework',
    'django_filters',
]

REST_FRAMEWORK = {
    'DEFAULT_FILTER_BACKENDS': (
        'django_filters.rest_framework.DjangoFilterBackend',
        ...
    ),
}

"""
Step 3:
Using in views
"""

class ProductList(generics.ListAPIView):
    queryset = Product.objects.all()
    filter_backends = (filters.DjangoFilterBackend,)
    filterset_fields = ('category', 'in_stock')  # This is the shortcut.