I'm having trouble noticing the difference between serializer and renderer in django rest framework.
I thought serializer is for converting the python object to JSON (or other data formats). But renderers, such as JSONRenders, are said to do the simillar thing, e.g. making(rendering) JSON object!
Could you tell me the difference between those two?
Renderers are used to serialize the response into a specific media type like JSON, XML, YAML, etc. Django REST Framework provides various built-in renderer classes and it also supports to write a custom renderer. We specify renderers as an iterable type (i.e tuple, list, set, etc.).
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.
The ModelSerializer class is the same as a regular Serializer class, except that: It will automatically generate a set of fields for you, based on the model. It will automatically generate validators for the serializer, such as unique_together validators.
Uses for serialization Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as: Sending the object to a remote application by using a web service.
A serializer doesn't know anything about any output formats. It just takes your models and converts then into python data structures It's then the job of the renderer to output that data in the appropriate format.
(A serializer also has the job of converting the posted data back to model instances, validating it in the process.)
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