Serializer vs Renderer in Django Rest Framework

Viewed 1728

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?

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

Serializers simply convert to python data structures.

Where Renderer has multiple purposes

  • JSONRenderer: Renders the request data into JSON, using utf-8 encoding.
  • TemplateHTMLRenderer: Renders data to HTML, using Django's standard template rendering.
  • StaticHTMLRenderer: A simple renderer that simply returns pre-rendered HTML.
  • BrowsableAPIRenderer: Renders data into HTML for the Browsable API.
  • AdminRenderer: Renders data into HTML for an admin-like display.
  • HTMLFormRenderer: Renders data returned by a serializer into an HTML form.
  • MultiPartRenderer: This renderer is used for rendering HTML multipart form data.

You may also make custom renderer.

See the link for details information.

Related