Skip to main content

Build clean APIs with DRF faster

Project description

Test Coverage

Django REST Framework Batteries

Build clean APIs with DRF faster.

Overview

Here's a quick overview of what the library has at the moment:

  • Action-based serializers for ViewSets
  • Two serializers per request/response cycle for ViewSets and GenericAPIViews
  • Action-based permissions for ViewSets
  • Single format for all errors

Requirements

  • Python ≥ 3.8
  • Django ≥ 3.2
  • Django REST Framework ≥ 3.12

Installation

$ pip install django-rest-batteries

Usage

Action-based serializers for ViewSets

Each action can have a separate serializer:

from rest_batteries.mixins import RetrieveModelMixin, ListModelMixin
from rest_batteries.viewsets import GenericViewSet
...

class OrderViewSet(RetrieveModelMixin,
                   ListModelMixin,
                   GenericViewSet):
    response_action_serializer_classes = {
        'retrieve': OrderSerializer,
        'list': OrderListSerializer,
    }

Two serializers per request/response cycle

We found that more often than not we need a separate serializer for handling request payload and a separate serializer for generating response data.

How to achieve it in ViewSet:

from rest_batteries.mixins import CreateModelMixin, ListModelMixin
from rest_batteries.viewsets import GenericViewSet
...

class OrderViewSet(CreateModelMixin,
                   ListModelMixin,
                   GenericViewSet):
    request_action_serializer_classes = {
        'create': OrderCreateSerializer,
    }
    response_action_serializer_classes = {
        'create': OrderResponseSerializer,
        'list': OrderResponseSerializer,
        'cancel': OrderResponseSerializer,
    }

How to achieve it in GenericAPIView:

from rest_batteries.generics import CreateAPIView
...


class OrderCreateView(CreateAPIView):
    request_serializer_class = OrderCreateSerializer
    response_serializer_class = OrderResponseSerializer

Action-based permissions for ViewSets

Each action can have a separate set of permissions:

from rest_batteries.mixins import CreateModelMixin, UpdateModelMixin, ListModelMixin
from rest_batteries.viewsets import GenericViewSet
from rest_framework.permissions import AllowAny, IsAuthenticated
...

class OrderViewSet(CreateModelMixin,
                   UpdateModelMixin,
                   ListModelMixin,
                   GenericViewSet):
    action_permission_classes = {
        'create': IsAuthenticated,
        'update': [IsAuthenticated, IsOrderOwner],
        'list': AllowAny,
    }

Single format for all errors

We believe that having a single format for all errors is good practice. This will make the process of displaying and handling errors much simpler for clients that use your APIs.

Any error always will be a JSON object with a message, code (identifier of the error), and field if the error is specific to a particular field. How your response could look like:

{
    "errors": [
        {
            "message": "Delete or cancel all reservations first.",
            "code": "invalid"
        },
        {
            "message": "Ensure this field has no more than 21 characters.",
            "code": "max_length",
            "field": "address.work_phone"
        },
        {
            "message": "This email already exists",
            "code": "unique",
            "field": "login_email"
        }
    ]
}

You will not have a single format out-of-the-box after installation. You need to add an exception handler to your DRF settings:

REST_FRAMEWORK = {
    ...
    'EXCEPTION_HANDLER': 'rest_batteries.exception_handlers.errors_formatter_exception_handler',
}

Credits

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

django_rest_batteries-1.4.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

django_rest_batteries-1.4.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file django_rest_batteries-1.4.1.tar.gz.

File metadata

  • Download URL: django_rest_batteries-1.4.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.12 Linux/5.15.0-1041-azure

File hashes

Hashes for django_rest_batteries-1.4.1.tar.gz
Algorithm Hash digest
SHA256 7ee0ce97c0c678dbc2949aba011d7170c61a525bece83986c7e8f5698698f768
MD5 98e12ebdb9f052287f4593011a4d784c
BLAKE2b-256 9027b1aa26973f4ffb8a7ee39f1b8d4346786d3dd61431195c52fbeab0f96230

See more details on using hashes here.

File details

Details for the file django_rest_batteries-1.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_rest_batteries-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 236918b0671bdb12fd13240ae56df78512d0552cb3de4799509164e3629ef20c
MD5 3f23ce5917bcff1d385d1fd9678e06da
BLAKE2b-256 19a02638f2da83ba198a76179e194b1ac3db5e4d2e780ceb5f7de42f17c2355e

See more details on using hashes here.

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