mardi 5 mai 2015

Django Rest Framework Form

What I want to do:

Django Rest Framework comes with a renderer for returning a HTML form from a serializer[1]. After poring through the docs and then through the code, I still cannot figure out how to get it to render a blank form.

What I think the problem might be:

I can't figure out how to instantiate a blank serializer. In the docs for DRF they say the serializer has a very similar API to django's forms. When you want a blank form in a template, you instantiate one with MyForm() and pass it to the template context. However when I try that with a serializer, the .data attribute is just {'detail': 'Not Found.'}

Please help

I feel like I must be missing something blindingly obvious because rendering a blank form seems like a pretty major use case for an HTMLFormRenderer.

Some Source Code

class CustomViewSet(ModelViewSet):
    lookup_field = 'id'
    pagination_class = MyCustomPagination
    serializer_class = MyModelSerializer
    renderer_classes = (JSONRenderer, BrowsableAPIRenderer, HTMLFormRenderer)

    def create(self, request, *args, **kwargs):
        # return an html form for GET requests to /api/my_model/create/.form

I'm pretty sure I have the urlconf wired up right that the request ends up in the right place, because when I GET /api/my_model/<pk>/.form I get a form in response, with the model attributes pre-entered. I just want a way to get a blank form...

Aucun commentaire:

Enregistrer un commentaire