Custom Models, Serializers, Backends and Views to facilitate saving filters in sessions.
Project description
DRF session filtering
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for drf-session-filtering-2.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dd29bff138a3bde5a36808d422883d2a8864a394676be25683c555410f02a1b |
|
MD5 | 8cda92221d67b1a0e3d34972800c4ba7 |
|
BLAKE2b-256 | 73fa91fbd52aeea31190bc337803b02470a54a5b8076bdb7b16d2630a3a8c895 |
Close
Hashes for drf_session_filtering-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b3fcbd208f1c00887ef041bd011428754588b8d1c459b794f39203da6673fb6 |
|
MD5 | 87687ad3154256f894f49004fe4adcf8 |
|
BLAKE2b-256 | bc162610e1b919e2580999577c274b44dab5c09f304b3ac3fc47b42890978bdd |