项目作者: evenicoulddoit

项目描述 :
Extensions to help DRY up Django Rest Framework serializers
高级语言: Python
项目地址: git://github.com/evenicoulddoit/django-rest-framework-serializer-extensions.git


Django REST framework serializer extensions

A collection of useful tools to DRY up your Django Rest Framework serializers

Full documentation: http://django-rest-framework-serializer-extensions.readthedocs.io/

build-status-image
coverage-status-image
pypi-version

Overview

Serializer extensions reduces the need for very similar serializers, by
allowing the fields to be defined on a per-view/request basis. Fields can be
whitelisted, blacklisted, and child serializers can be optionally expanded.
Whatever fields you choose to use, your querysets can be optimized
automatically
to make the fewest database calls possible.

Support for HashIds is
also provided. If you’re currently exposing your internal IDs over a public
API, we suggest you consider switching to HashIds instead.

:star: Lovingly open-sourced by Housekeep.

Requirements

Tested against:

  • Python (3.8, 3.9, 3.10, 3.11, 3.12)
  • Django (3.2, 4.2, 5.0)
  • Django REST Framework
    (3.12, 3.13, 3.14, 3.15). Note Django REST Framework 3.15 is the first version to
    officially support Django 4.2+.
  • HashIds (>1.0)

Installation

Install using pip:

  1. $ pip install djangorestframework-serializer-extensions

And add rest_framework_serializer_extensions to your INSTALLED_APPS setting:

  1. INSTALLED_APPS = (
  2. ...
  3. 'rest_framework_serializer_extensions'
  4. )

Basic Usage

To activate the serializer extensions, add the SerializerExtensionsMixin to your serializers:

  1. # serializers.py
  2. from rest_framework.serializers import ModelSerializer
  3. from rest_framework_serializer_extensions.serializers import SerializerExtensionsMixin
  4. ...
  5. class OwnerSerializer(SerializerExtensionsMixin, ModelSerializer):
  6. class Meta:
  7. model = models.Owner
  8. fields = ('id', 'name')
  9. expandable_fields = dict(
  10. organization=OrganizationSerializer,
  11. cars=dict(
  12. serializer=SkuSerializer,
  13. many=True
  14. )
  15. )

And add the SerializerExtensionsAPIViewMixin to your API views:

  1. from rest_framework.generics import RetrieveAPIView
  2. from rest_framework_serializer_extensions.views import SerializerExtensionsAPIViewMixin
  3. class RetriveOwnerAPIView(SerializerExtensionsAPIViewMixin, RetrieveAPIView):
  4. ...

Examples

Serializer extensions allows your API to re-use your serializers to fit a
variety of use cases. The examples shown below use query parameters to
modify the response, but individual views can interact with your serializers
in much the same way.

  1. >>> GET /owner/x4F/
  2. {
  3. "id": 'x4F',
  4. "name": 'tyrell',
  5. "organization_id": 'kgD'
  6. }
  1. >>> GET /owner/x4F/?expand=organization
  2. {
  3. "id": 'x4F',
  4. "name": 'tyrell',
  5. "organization_id": 'kgD',
  6. "organization": {
  7. "id": "kgD",
  8. "name": "E Corp"
  9. }
  10. }
  1. >>> GET /owner/x4F/?expand=cars__model&exclude=name
  2. {
  3. "id": 'x4F',
  4. "organization_id": 'kgD',
  5. "cars": [
  6. {
  7. "id": "wf9",
  8. "variant": "P100D",
  9. "model": {
  10. "id": "ncX",
  11. "name": "Model S"
  12. }
  13. }
  14. ]
  15. }
  1. >>> GET /owner/x4F/?expand=cars&only=cars__variant
  2. {
  3. "cars": [
  4. {
  5. "variant": "P100D",
  6. }
  7. ]
  8. }

Testing

Install testing requirements.

  1. $ pip install -e .[test]

Run with pytest

  1. $ pytest

You can also use the excellent
tox testing tool to run the
tests against all supported versions of Python and Django. Install tox
globally, and then simply run:

  1. $ tox

Documentation

To build the documentation, you’ll need to install mkdocs. These are included in the development extras:

  1. $ pip install -e .[dev]

To preview the documentation:

  1. $ mkdocs serve
  2. Running at: http://127.0.0.1:8000/

To build the documentation:

  1. $ mkdocs build