
In Django Rest Framework, the process of content negotiation enables the API server and client to agree on the format of the data being exchanged actively. During this process, they mutually determine the data format and reach a consensus.
Django Rest Framework offers several built-in content negotiation classes to manage contentNegotiation.
Types of Content Negotiation
These classes determine which format to use based on the content type of the request. Some of the built-in classes include:
JSONRenderer
:
The data rendered in JSON format using this class. To activate it, the JSONRenderer
class can be used.
BrowsableAPIRenderer
:
Developers find this class useful for visualizing the data as it renders data in HTML format.
XMLRenderer
:
This class can render the data in XML format.
YAMLRenderer
:
This class can render the YAML format.
To use contentNegotiation in Django Rest Framework, you can set the DEFAULT_RENDERER_CLASSES
setting in your settings.py
file to a list of the renderer classes you want to use. For example, to use JSON and HTML formats, you can set the following:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
]
}
Once you have set the DEFAULT_RENDERER_CLASSES
setting, Django Rest Framework will automatically handle contentNegotiation based on the content type of the request.
Django Rest Framework enables creating custom contentNegotiation classes for handling various formats or modifying the behavior of built-in classes.
Custom contentNegotiation classes must inherit from the BaseRenderer
class and implement the render
method.
Here’s an example of a custom content negotiation class that renders data in CSV format:
from rest_framework.renderers import BaseRenderer
import csv
class CSVRenderer(BaseRenderer):
media_type = 'text/csv'
format = 'csv'
def render(self, data, media_type=None, renderer_context=None):
header = data[0].keys()
rows = [row.values() for row in data]
response = csv.writer(self.charset)
response.writerow(header)
for row in rows:
response.writerow(row)
return ''
The CSVRenderer class is an example of content negotiation in Django Rest Framework. It inherits from the BaseRenderer class and implements the render method to render data in CSV format. The media_type attribute specifies the MIME type of the response, while the format attribute specifies the format of the response. The render method takes in data, media type, and renderer context as parameters, and returns data in the desired format when invoked.
To use this custom content negotiation class, you can add it to the DEFAULT_RENDERER_CLASSES
setting in your settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
'path.to.CSVRenderer',
]
}
With this configuration, Django Rest Framework will automatically handle content negotiation for CSV format based on the content type of the request.