我很确定这个问题的答案显然是“否”,因为 Django mixins 应该
继承“对象”,但我找不到我的问题的替代解决方案:(
为了使问题尽可能简单,,,
视图.py
class JSONResponseMixin(object):
def render_to_response(self, context):
"Returns a JSON response containing 'context' as payload"
return self.get_json_response(self.convert_context_to_json(context))
def get_json_response(self, content, **httpresponse_kwargs):
"Construct an `HttpResponse` object."
return http.HttpResponse(content,
content_type='application/json',
**httpresponse_kwargs)
def convert_context_to_json(self, context):
"Convert the context dictionary into a JSON object"
# Note: This is *EXTREMELY* naive; in reality, you'll need
# to do much more complex handling to ensure that arbitrary
# objects -- such as Django model instances or querysets
# -- can be serialized as JSON.
return json.dumps(context)
class HandlingAJAXPostMixin(JSONResponseMixin):
def post(self, request, *args, **kwargs):
.....
data = {'somedata': somedata}
return JSONResponseMixin.render_json_response(data)
class UserDetailView(HandlingAJAXPostMixin, DetailView):
model = MyUser
.....
所以我遇到的问题是,对于多个视图,我想用相同的
JSON 响应来响应他们的“发布”请求。这就是为什么我定义了HandlingAJAXPostMixin,以便我可以将它重用于
其他视图。由于HandlingAJAXPostMixin 返回一个JSON 响应,
它需要一个render_json_response 方法,该方法在JSONResponseMixin 中定义。
这就是我让HandlingAJAXPostMixin继承
JSONResponseMixin的原因,但这显然是错误的:(..
有
什么建议吗..?
谢谢!!!