django-rest-framework - autogenerate form in browsable API?
Asked Answered
T

4

15

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 to how we define ModelForms. This would allow us to more easily test input to the API in some cases. I'm currently using ModelSerializers and the generic view APIView in case that makes a difference.

I have read the documentation (several times at this point) but didn't see it mentioned anywhere.

screenshot

Tintype answered 31/1, 2013 at 0:31 Comment(1)
I could still not get the exact form. Here is what i can see as of now!enter image description hereNonviolence
C
19

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.

Contradictory answered 31/1, 2013 at 13:32 Comment(3)
ahh dammit. was confused going through the tutorial here: django-rest-framework.org/tutorial/3-class-based-views.html I was just using the APIView and not actually using the standard generic views like they do at the very bottom. Thanks!Tintype
You can do the same on APIView. Just set the serializer_class and it will be used for the form in the browsable apiIntuition
@Intuition comment is actually an answer when using APIView.Seriocomic
I
6

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

class MyView(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

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
Intuition answered 9/12, 2016 at 15:39 Comment(0)
S
0
class MyApiView(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

Sogdian answered 2/4, 2020 at 23:2 Comment(0)
H
0
  1. Make sure you have FormParser in your REST_FRAMEWORK["DEFAULT_PARSER_CLASSES"] (If you don't have this parameter declared, this won't be needed).
REST_FRAMEWORK = {
    # ...
    "DEFAULT_PARSER_CLASSES": [
        # ...
        "rest_framework.parsers.FormParser",
        # ...
    ]
    # ...
}
  1. Declare the serializer_class attribute or define the get_serializer_class method in your ViewSet.
class SomeInsaneViewSet(GenericViewSet, CreateModelMixin):
    # ...
    serializer_class = SomeModelSerializer
    # ...
class SomeInsaneViewSet(GenericViewSet, CreateModelMixin):
    # ...
    def get_serializer_class(self):
        match self.action:
            case "create": return SomeModelSerializer
        return None
    # ...
Hypoxia answered 22/2 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.