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/
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.
⭐ Lovingly open-sourced by Housekeep.
Requirements
Tested against:
- Python (3.6, 3.7, 3.8)
- Django (2.1, 2.2, 3.0)
- Django REST Framework (3.9, 3.10, 3.11)
- HashIds (>1.0)
Installation
Install using pip:
$ pip install djangorestframework-serializer-extensions
And add rest_framework_serializer_extensions to your INSTALLED_APPS setting:
INSTALLED_APPS = (
...
'rest_framework_serializer_extensions'
)
Basic Usage
To activate the serializer extensions, add the SerializerExtensionsMixin to your serializers:
# serializers.py
from rest_framework.serializers import ModelSerializer
from rest_framework_serializer_extensions.serializers import SerializerExtensionsMixin
...
class OwnerSerializer(SerializerExtensionsMixin, ModelSerializer):
class Meta:
model = models.Owner
fields = ('id', 'name')
expandable_fields = dict(
organization=OrganizationSerializer,
cars=dict(
serializer=SkuSerializer,
many=True
)
)
And add the SerializerExtensionsAPIViewMixin to your API views:
from rest_framework.generics import RetrieveAPIView
from rest_framework_serializer_extensions.views import SerializerExtensionsAPIViewMixin
class RetriveOwnerAPIView(SerializerExtensionsAPIViewMixin, RetrieveAPIView):
...
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.
>>> GET /owner/x4F/
{
"id": 'x4F',
"name": 'tyrell',
"organization_id": 'kgD'
}
>>> GET /owner/x4F/?expand=organization
{
"id": 'x4F',
"name": 'tyrell',
"organization_id": 'kgD',
"organization": {
"id": "kgD",
"name": "E Corp"
}
}
>>> GET /owner/x4F/?expand=cars__model&exclude=name
{
"id": 'x4F',
"organization_id": 'kgD',
"cars": [
{
"id": "wf9",
"variant": "P100D",
"model": {
"id": "ncX",
"name": "Model S"
}
}
]
}
>>> GET /owner/x4F/?expand=cars&only=cars__variant
{
"cars": [
{
"variant": "P100D",
}
]
}
Testing
Install testing requirements.
$ pip install -r requirements.txt
Run with runtests.
$ ./runtests.py
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:
$ tox
Documentation
To build the documentation, you’ll need to install mkdocs.
$ pip install mkdocs
To preview the documentation:
$ mkdocs serve
Running at: http://127.0.0.1:8000/
To build the documentation:
$ mkdocs build
Metadata
Release files for djangorestframework-serializer-extensions 2.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| djangorestframework-serializer-extensions-2.0.1.tar.gz | 14.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| djangorestframework_serializer_extensions-2.0.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 46.0 kB
Release files / djangorestframework-serializer-extensions-2.0.1.tar.gz
| Download URL | djangorestframework-serializer-extensions-2.0.1.tar.gz |
|---|---|
| Size | 14.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d047fd71afd18647def604d7f6b2d4253ee90fd90c2ccf0a352d176ffed4b6a5
|
|
BLAKE2b-256 checksum How to use checksums |
4b4177dd0e5f1b5ae1f31900f27b7cbf5c9246b24de8ecac97db7cba2d691015
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.9
|
Release files / djangorestframework_serializer_extensions-2.0.1-py2.py3-none-any.whl
| Download URL | djangorestframework_serializer_extensions-2.0.1-py2.py3-none-any.whl |
|---|---|
| Size | 31.4 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
22f3b7e06eb694ed7f1ff16623838a6d37032e6124b3a03badae1fdddd3247fa
|
|
BLAKE2b-256 checksum How to use checksums |
b9cd2ed7357ee693497f1ec55ac24dda142b057a332b5793fc6da90c00f12d75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.7.9
|