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.
Metadata
Release files for drf-session-filtering 2.0.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-session-filtering-2.0.0.tar.gz | 7.2 kB | Without provenance | Details for drf-session-filtering-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_session_filtering-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for drf_session_filtering-2.0.0-py3-none-any.whl |
Total release size: 15.9 kB
Release files / drf-session-filtering-2.0.0.tar.gz
| Download URL | drf-session-filtering-2.0.0.tar.gz |
|---|---|
| Size | 7.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5dd29bff138a3bde5a36808d422883d2a8864a394676be25683c555410f02a1b
|
|
BLAKE2b-256 checksum How to use checksums |
73fa91fbd52aeea31190bc337803b02470a54a5b8076bdb7b16d2630a3a8c895
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|
Release files / drf_session_filtering-2.0.0-py3-none-any.whl
| Download URL | drf_session_filtering-2.0.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b3fcbd208f1c00887ef041bd011428754588b8d1c459b794f39203da6673fb6
|
|
BLAKE2b-256 checksum How to use checksums |
bc162610e1b919e2580999577c274b44dab5c09f304b3ac3fc47b42890978bdd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|
Security for drf-session-filtering 2.0.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.