
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
drf-payload-customizer
Advanced tools
This package allows you to customize your django-rest-framework serializer i/o to make modern frontend frameworks happy.
This package allows you to customize your django-rest-framework
serializer i/o to make modern frontend frameworks happy.
The following modifications are supported:
keys
in API output to camelCase
. Also
on reception of an input on an API, transform all inputs to snake_case
.key
in your API schema. (works in both
direction).""
with None
on 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 parameter PAYLOAD_TRANSFORM_NESTED
(see tests) which you can set
to True
in your Serializer.Meta
class 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 a field_mappings
dict in your Serializer.Meta
class.PayloadNoNullOrNoneMixin
: Perform nullify/blankify (3) above.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.
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
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
FAQs
This package allows you to customize your django-rest-framework serializer i/o to make modern frontend frameworks happy.
We found that drf-payload-customizer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.