How can I update/append serializer.data
in Django Rest Framework?
data = serializer.data.update({"item": "test"}) not working
return Response(serializer.data, status=status.HTTP_201_CREATED)
serializer.data
is <class 'rest_framework.utils.serializer_helpers.ReturnDict'>
The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.
Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.
Django's serialization framework provides a mechanism for “translating” Django models into other formats. Usually these other formats will be text-based and used for sending Django data over a wire, but it's possible for a serializer to handle any format (text-based or not).
Unfortunately, serializer.data
is a property of the class and therefore immutable. Instead of adding items to serializer.data
you can copy serializer.data
to another dict
. You can try this:
newdict={'item':"test"} newdict.update(serializer.data) return Response(newdict, status=status.HTTP_201_CREATED)
Read more about property
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With