Mix APIView and FormMixin: 'function' object has no attribute 'copy'
Question: I want to make a class based view that has GET method which renders a form and
POST method which process form data and also accept JSON. So I use Django REST
Framework's APIView and Django's FormMixin like this:

    
    
    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})
    

When `get` method is called, I get this error:

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

Traceback:

    
    
    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` is an empty dictionary defined in FormMixin class like this:

    
    
    initial = {}
    

Why it suddenly lost its `copy` method? Or what does exception means?
Exception won't appear if I replace `APIView` by some Django built-in class
like `View`. Can I use APIView class and FormMixin or I should change my way
to render form?


Answer: The reason this happens is that `GenericAPIView` already has a method called
`initial`.

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

First thing's first - you should not add FormMixin into DRF generic view, they
are not intended to work together. You should use `HTMLFormRenderer` as your
renderer instead. Please refer to <http://www.django-rest-
framework.org/topics/html-and-forms/#rendering-forms> for more details.

Also, think about splitting your json endpoints by using prefixed urls, smth
like `api/v1/`. This will save you a lots of headaches overtime, when dealing
with more complicated logic.

