Skip to main content

Custom Models, Serializers, Backends and Views to facilitate saving filters in sessions.

Project description

DRF session filtering

example workflow Coverage Status

About

This package provides a Custom Models, Serializers, Backends and Views to facilitate saving filters in sessions.

Install

pip install drf-session-filtering

Usage

Define a SESSION_MODEL_CONTAINER_KEY setting:

# settings.py

SESSION_MODEL_CONTAINER_KEY = 'session_objects'

Define a models and filter models that will be stored in session.

# models.py 

from django.db import models

from session_filtering.models import BaseSessionModel


class Book(models.Model):
    title = models.CharField(max_length=100)
    issue_year = models.IntegerField()
    publisher = models.TextField()
    price = models.FloatField()
    ...

class BookFilterSessionModel(BaseSessionModel):
   pass

Define a filter set.

# filters.py

from django_filters.rest_framework import FilterSet

from .models import Book


class BookFilter(FilterSet):
    
    class Meta:
        model = Book
        fields = {
            'title': ['exact', 'icontains'],
            'issue_year': ['gt'],
            'price': ['lt'],
            'publisher': ['exact', 'contains'],
        }

Define a serializers for models and filter models.

# serializers.py

from rest_framework import serializers
from session_filtering.serializers import (
    FilterBaseSerializer,
    SessionSaveFilterMixin,
)

from .filters import BookFilter
from .models import Book


class BookSerializer(serializers.ModelSerializer):

    class Meta:
        model = Book
        fields = '__all__'


class BookFilterSerializer(SessionSaveFilterMixin, FilterBaseSerializer):

    class Meta:
        filter_class = BookFilter

Define a filter view and a model viewset using filter serializer and filter model defined before.

# views.py

from rest_framework.generics import GenericAPIView
from rest_framework.mixins import *
from rest_framework.viewsets import *

from session_filtering.views import BaseFilterMixin
from session_filtering.backends import SessionFilterBackend

from .filters import BookFilter
from .models import Book, BookFilterSessionModel
from .serializers import BookSerializer, BookFilterSerializer

    
class BookFilterViewSet(
    BaseFilterMixin,
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    ListModelMixin,
    GenericViewSet,
):
    filter_class = BookFilter
    serializer_class = BookFilterSerializer
    filter_session_model = BookFilterSessionModel

    
class BooksViewSet(
    CreateModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
    DestroyModelMixin,
    ListModelMixin,
    ViewSetMixin,
    GenericAPIView,
):
    serializer_class = BookSerializer
    filter_backends = [SessionFilterBackend]
    filter_class = BookFilter
    filter_session_model = BookFilterSessionModel
    filter_serializer_class = BookFilterSerializer
    filter_lookup_field = 'filterset_id'
    queryset = Book.objects.all()

License

The Django Wicked Historian package is licensed under the FreeBSD 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

drf-session-filtering-2.0.0.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

drf_session_filtering-2.0.0-py3-none-any.whl (8.7 kB view hashes)

Uploaded Python 3

Supported by

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