Skip to main content

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

Project description

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

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_addons_plus-0.4.tar.gz (21.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_addons_plus-0.4-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file drf_addons_plus-0.4.tar.gz.

File metadata

  • Download URL: drf_addons_plus-0.4.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for drf_addons_plus-0.4.tar.gz
Algorithm Hash digest
SHA256 d49fd5412e3167d57f4814110e7ddb39e53164333f12bd4e37c19e6c17268023
MD5 6f330891dad5bd306741c2c962777df1
BLAKE2b-256 710ac32687bb130a3ea24fa0df730bcadc216c719246e6dd1b19c61f7ca7727e

See more details on using hashes here.

File details

Details for the file drf_addons_plus-0.4-py3-none-any.whl.

File metadata

  • Download URL: drf_addons_plus-0.4-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for drf_addons_plus-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c3d8e98aa6c706777158886489fa4dbc6f7ab891279bbfa5a5cc77ab30ca7bf6
MD5 c25948913faa9023f29d932ea0f62d22
BLAKE2b-256 750ae8c854448a634ac076223982fd5aeb07df4360380999ee54367716045b81

See more details on using hashes here.

Supported by

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