django-rest-framework - autogenerate form in browsable API?

前端 未结 3 1337
北荒
北荒 2021-01-04 10:19

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 s

3条回答
  •  日久生厌
    2021-01-04 11:01

    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
    

提交回复
热议问题