Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializer vs Renderer in Django Rest Framework

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?

like image 447
Rhee Avatar asked Jan 07 '19 05:01

Rhee


People also ask

What is renderer in Django REST framework?

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.).

Do we need Serializers in Django REST framework?

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.

What is the difference between serializer and model serializer?

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.

When would you use a serializer?

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.


1 Answers

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.)

like image 101
Daniel Roseman Avatar answered Sep 19 '22 12:09

Daniel Roseman