Skip to main content

drf-addons-plus

Some magic to the base Django REST Framework


Overview

This project provides some extra functionalities to be used with Django REST Framework

  • Field selection to Viewsets, eg. ?fields=id,name
  • Conditional filtering to Viewsets, eg. ?conditional=active,-inactive
  • Table hashing and 304 responses

You can also just copy the code manually and implement it yourself


Requirements

  • Python 3.8+
  • Django 4.2, 5.0, 5.1
  • Django REST Framework 3.0+ or less, you can test it
  • Celery 3.4 or less, you can test it

Installation

Install using pip...

pip install drf_addons_plus

Examples

  • models.py:
from django.db import models

class FooModel(models.Model):
    name = models.CharField(max_length=100)
    status = models.BooleanField()
    category = models.CharField(choices=['foo', 'bar'])
  • serializers.py:
from drf_addons_plus import serializers

from .models import FooModel


class FooSerializer(serializers.DynamicFieldsModelSerializer):
    class Meta:
        model = FooModel
        fields = ['name', 'status', 'category']
  • views.py:
from rest_framework import filters
from drf_addons_plus import filters as filters_plus
from drf_addons_plus import viewsets

from .models import FooModel
from .serializers import FooSerializer


class FooViewSet(viewsets.FieldsModelViewSet):
    queryset = FooModel.objects.all()
    serializer_class = FooSerializer
    permission_classes = []
    filter_backends = [filters.SearchFilter, filters_plus.ConditionalFilter, filters_plus.FieldsFitlter]
    search_fields = ['name']
    conditional_fields = ['status']
    filter_fields = ['category']
  • Request http://localhost:8000/foo/:
[
    {
        "name": "Bar",
        "status": false,
        "category": "Foo",
    }
]
  • Request http://localhost:8000/foo/?fields=name&conditional=-status:
[
    {
        "name": "Bar"
    }
]
  • Request http://localhost:8000/foo/?conditional=status:
[]
  • Request http://localhost:8000/foo/?category="foo":
[
    {
        "name": "Bar",
        "status": false,
        "category": "Foo",
    }
]

Hashing for caching (WIP)

  • views.py:
...
class FooViewSet(viewsets.HashesViewSet, viewsets.FieldsModelViewSet):
    ...
    hash_column = "name" # Default is `updated_at`
...
  • Calculates hash on Create, Update or Delete
  • Responses HEADER X-Data-Hash on Listing
  • If HEADER X-Data-Hash passed on request, if matches current hash, returns 304, else list normally and returns new hash value
  • Use this for caching your client / front-end (like ReactJS) / front-end server (Like NextJS with KeyDB redis)

Disclaimer

This project has started and maybe will be maintened, but it's simple enough to probably not give you any trouble. USE IT AT YOUR OWN RISK

Build

  • Build
python setup.py sdist bdist_wheel
  • Publish
twine upload dist/*

Credits

Django REST Framework YAtOff

Metadata

Release files for drf-addons-plus 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-addons-plus 0.4.1
File Size Uploaded
drf_addons_plus-0.4.1.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-addons-plus 0.4.1
File Interpreter ABI Platform
drf_addons_plus-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 43.4 kB

Release files / drf_addons_plus-0.4.1.tar.gz

Download URL drf_addons_plus-0.4.1.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f87a81ebc9edcbefe3d6aab8739b642687458a13a516d22adacc6ebd50c9a88c
BLAKE2b-256 checksum
How to use checksums
d97b5cac8c8935ea5508720662b86ba61371315bb5ddd104b6b2b2096c3a0e97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / drf_addons_plus-0.4.1-py3-none-any.whl

Download URL drf_addons_plus-0.4.1-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
187cecf54102f828a15ae1c1d268ee1bb40868af03a08d13e37d2b78cc53368e
BLAKE2b-256 checksum
How to use checksums
a9ddd001ce2b93e357737a4e17c91869e04dca4effa5071a50ef08ab40021009
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4

2 release files

0.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page