To implement a custom media type for your API responses, inherit from ninja.renderers.BaseRenderer and override the render method. You must also define a media_type attribute on your class to set the Content-Type header. Finally, pass an instance of your renderer to the NinjaAPI constructor using the renderer argument.
The render method signature is:
render(self, request, data, *, response_status)
Arguments:
request: The HttpRequest object.data: The object that needs to be serialized.response_status: An int representing the HTTP status code to be returned.
from ninja import NinjaAPI
from ninja.renderers import BaseRenderer
class MyRenderer(BaseRenderer):
media_type = "text/plain"
def render(self, request, data, *, response_status):
return ... # your serialization here
api = NinjaAPI(renderer=MyRenderer())