drf-payload-customizer
This package allows you to customize your django-rest-framework serializer i/o to make modern frontend frameworks happy.
The following modifications are supported:
- Transformation: Convert all
keysin API output tocamelCase. Also on reception of an input on an API, transform all inputs tosnake_case. - Translation: Rename a
keyin your API schema. (works in both direction). - Nullify/Balankify: Replace
""withNoneon output direction and vice versa in the other direction.
We introduce 3 seperate Mixins that can you can subclass your APIView
from to achieve 1-3. They are:
PayloadTransformationMixin: Perform transformation (1) above. Has an optional parameterPAYLOAD_TRANSFORM_NESTED(see tests) which you can set toTruein yourSerializer.Metaclass to recursively convert all nested dictionaries. This feature only works in the output direction.PayloadTranslationMixin: Perform translation (2) above. You can specify custom mappings using afield_mappingsdict in yourSerializer.Metaclass.PayloadNoNullOrNoneMixin: Perform nullify/blankify (3) above.
Requirements
The mixin requires you to have the following dependencies:
djangorestframework==3.8.2
We are positive it would work with other versions of Django Rest Framework
as well. However, you will require Django==2.1.2 to run the unit tests.
Use it in your project
You can use all three of the mixins together like this in your project using
our drf_payload_customizer.mixins.PayloadConverterMixin mixin. The mixin
performs (1-3) modifications listed above. Here is how it is implemented:
class PayloadConverterMixin(PayloadTransformationMixin,
PayloadTranslationMixin,
PayloadNoNullOrNoneMixin):
"""
Use this mixin in all of our Serializers, to convert the JSON into a
format that is easier consumed by modern front-ends.
"""
def to_representation(self, instance):
return super().to_representation(instance)
def to_internal_value(self, camel_cased):
return super().to_internal_value(camel_cased)
Now, subclass your APIViews with PayloadConverterMixin as given below:
from drf_payload_customizer.mixins import PayloadConverterMixin
class CustomTestModelSerializer(PayloadConverterMixin, ModelSerializer):
class Meta:
model = TestModel
fields = ('parama', 'param_b',)
PAYLOAD_TRANSFORM_NESTED = True
# The mapping is the snake_case of your expected o/p
field_mappings = {
'parama': 'param_a'
}
# Now your serializer would output:
test_serializer = CustomTestModelSerializer(obj)
JSONRenderer().render(test_serializer.data)
> {'paramA': '', 'paramB': None}
# Your serializer also admits input in the same format:
content = json.dumps({'paramA': 'testA', 'paramB': 3}).encode()
stream = BytesIO(content)
data = JSONParser().parse(stream)
serializer = CustomTestModelSerializer(data=data)
self.assertTrue(serializer.is_valid())
> True
To run tests
cd drf_payload_customizer
# Make sure you change to a virutal environment according to your project setup
python setup.py install
pip install -r requirements.txt
cd tests/test_app/
python manage.py test
Metadata
Release files for drf-payload-customizer 0.1.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 | Provenance | Actions |
|---|---|---|---|---|
| drf-payload-customizer-0.1.1.tar.gz | 8.9 kB | Without provenance | Details for drf-payload-customizer-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_payload_customizer-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for drf_payload_customizer-0.1.1-py3-none-any.whl |
Total release size: 19.6 kB
Release files / drf-payload-customizer-0.1.1.tar.gz
| Download URL | drf-payload-customizer-0.1.1.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7fc578e3b871a93e771cdd36d038794f623db06da92c739596db19726728824
|
|
BLAKE2b-256 checksum How to use checksums |
0d60051a4fc19cdd960c83a5e25d6416dcbd90ffe054fd9d95f7a79263263220
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / drf_payload_customizer-0.1.1-py3-none-any.whl
| Download URL | drf_payload_customizer-0.1.1-py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2872582c90173da21d18274ce9190c12d08dc07643829d1cd93b54c06c3e3f63
|
|
BLAKE2b-256 checksum How to use checksums |
b103b226daac2d68f5055d04c29bfcf399ac4b0acbb79dd2e372ec76ba1ff6bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for drf-payload-customizer 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.