首页 > 解决方案 > Django-rest-framework 使用 3 种内容类型如何管理渲染器和序列化器

问题描述

我有一个带有django-rest-framework(最新版本)的 api,我需要根据所content-type问的不同响应。

现在,我期望application/vnd.api+jsontext/html(用于可浏览的 api)。但我需要为application/vnd.oasis.stix+json; version=2.0.

我试图添加一个新的自定义渲染器,但它只允许我指定“application/json”。我已经创建了这个,但它需要所有json内容。

class StixRenderer(JSONRenderer):
    media_type = 'application/vnd.oasis.stix+json'

    def render(self, data, media_type=None, renderer_context=None):
        return super(StixRenderer, self).render(data, media_type, renderer_context)

我知道我可以发送不同的序列化程序,但我不知道如何区分不同的内容类型。

标签: djangopython-3.xdjango-rest-framework

解决方案


推荐阅读