【问题标题】:What is JsonResponseMixin? How they work with the class based views什么是 JsonResponseMixin?他们如何使用基于类的视图
【发布时间】:2018-01-13 07:32:42
【问题描述】:

我读过 mixin 并且明白 mixin 是一种特殊的多重继承,并为一个类提供了很多可选的特性。 现在我想知道什么是 JsonResponseMixin 以及为什么使用它们。

class JsonResponseMixin(object):

    def render_to_json_response(self,context,**response_kwargs):
        return JsonResponse(context,**response_kwargs)

    def get_data(self,context):
        return context

这是我在 mixin.py 中找到的代码。有人能解释一下为什么使用它吗。它们用于序列化数据吗?请详细说明

【问题讨论】:

    标签: python json django mixins


    【解决方案1】:

    发送 Json 响应基本上是一种抽象。如果要发送有效的 Json 响应,则需要将数据转换为 Json 并发送正确的标头。

    在这种情况下,JsonResponseMixin 会为你做这件事,你只需要返回一个 json 可序列化对象。

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2019-08-29
      相关资源
      最近更新 更多