Skip to content Skip to sidebar Skip to footer

Django-rest-framework - Autogenerate Form In Browsable Api?

Not sure if i'm using the right vocabulary. In the browsable api that comes for free with django-rest-framework, I was wondering if there was a way to autogenerate a form similar t

Solution 1:

If you're using the generic class-based-views you'll get that for free. Try the live tutorial at http://restframework.herokuapp.com logging in as one of the users, so that you can create some snippets. eg user: 'max', password: 'max'.

Any views subclassing GenericAPIView and setting a serializer_class will get that behavior, as REST framework can determine what the form should look like.

For example:

screenshot of form input

(Note the form input at the bottom of the screen shot)

If you're just working from APIView you'll get the generic content input (such as json), like the once you've included a screenshot of, which is also useful, but not quite as convenient as the forms.

Solution 2:

Create a serialiser class that fits the form input fields you want and set it on your APIView like so;

classMyView(APIView):
    serializer_class = MySerializer  # Used for the form in the browsable api

That works just perfectly.

Example of a serializer class based on a model:

from rest_framework import serializers

classMySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel

Solution 3:

classMyApiView(APIView):
    """My Demo API View"""
    serializer_class = serializers.MySerializers

Make sure you're using the name "serializer_class" and not any other name like serializers_class.

using the exact "serializer_class" will autogenerate form in the browseable API

Post a Comment for "Django-rest-framework - Autogenerate Form In Browsable Api?"