mehmet
mehmet

Reputation: 8144

Rendering a form with django rest framework's ModelViewSet class insead of APIView

I want to create boiler-plate forms for my django models using the rest framework.

Documentation shows it using the APIView: http://www.django-rest-framework.org/topics/html-and-forms/#rendering-forms.

But I want to use the ModelViewSet in order to avoid defining custom action methods.

Is this possible? Can you share an example?

Here is what I tried. My model:

class Person(AbstractUser):
    pass

My serializer:

class PersonSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Person
        fields = ('first_name', 'last_name', 'email', 'groups')

My view:

class PersonViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows persons to be viewed or edited.
    """
    queryset = Person.objects.all().order_by('-date_joined')
    serializer_class = PersonSerializer
    renderer_classes = [TemplateHTMLRenderer]
    template_name = 'common/rest_create.html'

And here is my url:

url(r'person_create_api/$', PersonViewSet.as_view({'get': 'create'}), name='person-create-api'),

And the error I get:

IntegrityError at /school/person_create_api/
duplicate key value violates unique constraint "school_person_username_key"
DETAIL:  Key (username)=() already exists.

When I add the username field to the serializer fields, I get:

HTTP/1.0 400 Bad Request
Date: Tue, 20 Sep 2016 17:00:22 GMT
Server: WSGIServer/0.2 CPython/3.5.1+
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Allow: GET, HEAD, OPTIONS
Content-Type: text/html; charset=utf-8

I am using django 1.9 and latest DRF 3.4.6.

Upvotes: 4

Views: 4476

Answers (1)

mehmet
mehmet

Reputation: 8144

First thing first, let the DRF create the url's for you (this prevents misconfiguring the urls):

from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'snippets', PersonViewSet)

urlpatterns = [
    ...
    url(r'^', include(router.urls)),
]

Here are the urls generated:

^persons/$ [name='person-list']
^persons\.(?P<format>[a-z0-9]+)/?$ [name='person-list']
^persons/blank_form/$ [name='person-blank-form']
^persons/blank_form\.(?P<format>[a-z0-9]+)/?$ [name='person-blank-form']
^persons/(?P<pk>[^/.]+)/$ [name='person-detail']
^persons/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='person-detail']

Model is same as above, and here is the view:

class PersonViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows persons to be viewed or edited.
    """
    queryset = Person.objects.all().order_by('-date_joined')
    serializer_class = PersonSerializer
    template_name = 'common/rest_create.html'

    @list_route(renderer_classes=[renderers.TemplateHTMLRenderer])
    def blank_form(self, request, *args, **kwargs):
        serializer = PersonSerializer()
        return Response({'serializer': serializer})

Note, the TemplateHtmlRenderer is set at the method level instead of globally in the class so to let it use other appropriate renderers for other methods/views. Cheating off the tutorial at DRF site, serializer = PersonSerializer() this is used for generating an unbound form.

And here is the template:

{% load rest_framework %}

<html><body>    
<h1>New Person</h1>

<form action="{% url 'school:person-create' %}" method="POST">
    {% csrf_token %}
    {% render_form serializer %}
    <input type="submit" value="Save">
</form>

</body></html>

Other views that you expect from a ModelViewSet work as usual.

Upvotes: 3

Related Questions