混合APIView和FormMixin:'function'对象没有属性'copy'

时间:2015-10-31 16:20:59

标签: python django django-rest-framework

我想创建一个基于类的视图,它具有GET方法,该方法呈现一个表单和POST方法,它处理表单数据并接受JSON。所以我像这样使用Django REST Framework的APIView和Django的FormMixin:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import TemplateHTMLRenderer, JSONRenderer
from django.views.generic.edit import FormMixin
from django.core.urlresolvers import reverse_lazy
from feedback.models import Feedback

class FeedbackView(GenericAPIView, FormMixin):
    renderer_classes = (TemplateHTMLRenderer, JSONRenderer)
    template_name = 'feedback/feedback.html'
    form_class = FeedbackForm
    success_url = reverse_lazy('feedback')

    def get(self, request, *args, **kwargs):
        form = self.get_form()
        return Response({'form': form})

调用get方法时,出现此错误:

Exception Type: AttributeError at /feedback/
Exception Value: 'function' object has no attribute 'copy'

回溯:

File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/rest_framework/views.py" in dispatch
  466.             response = self.handle_exception(exc)
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/rest_framework/views.py" in dispatch
  463.             response = handler(request, *args, **kwargs)
File "/home/roman/work/gvis_website/feedback/views.py" in get
  62.         form = self.get_form()
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/views/generic/edit.py" in get_form
  74.         return form_class(**self.get_form_kwargs())
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/views/generic/edit.py" in get_form_kwargs
  81.             'initial': self.get_initial(),
File "/home/roman/.virtualenvs/env__gvis_website/lib/python3.3/site-packages/django/views/generic/edit.py" in get_initial
  54.         return self.initial.copy()

initial是FormMixin类中定义的空字典,如下所示:

initial = {}

为什么它突然失去了copy方法?或者异常是什么意思? 如果我用APIView之类的Django内置类替换View,则不会出现异常。 我可以使用API​​View类和FormMixin,还是应该改变渲染形式的方式?

1 个答案:

答案 0 :(得分:0)

发生这种情况的原因是GenericAPIView已经有一个名为initial的方法。

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/views.py#L369

第一件事是第一件事 - 您不应该将FormMixin添加到DRF通用视图中,它们不能一起工作。您应该使用HTMLFormRenderer作为渲染器。有关详细信息,请参阅http://www.django-rest-framework.org/topics/html-and-forms/#rendering-forms

另外,考虑使用前缀网址来分割你的json端点,像api/v1/一样。在处理更复杂的逻辑时,这将为您节省很多麻烦。