0
votes

Is there a way to define the object for a post function in the schema of django rest framework?

I have a rest api view:

class DocumentView(viewsets.ModelViewSet):
    ...
    queryset = Document.objects.all().order_by('pk')
    serializer_class = serializers.DocumentSerializer
    pagination_class = LargeResultPagination
    permission_classes = (IsAuthenticated,)
    authentication_class = (
        Token,
    )

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)  

The data that should be posted is of format:

{
     "items": ["a", "b", "c"],
     "name": "test" 
}

How can I describe that in the schema using coreapi?

manual_fields=[
        coreapi.Field(
            "data",
            required=True,
            location="body",
            description='{"items":[], "name":str}',
            schema=coreschema.Object()
        ),
    ])

So at least it will be in the description. This of course is not very convinient, especially igf you need more fields in the to-be-posted JSON, since the description is single-line only in swagger.

The most ideal situation would be if I could get the 'items' and the 'name' field as separate fields in the swagger page.

I've googled, but I cannot find an answer for this. Al the examples I saw are for get parameters. So what is best practice here?

2
Is there a way to have this JSON as the Example value for the body in the POST API? - Shashank Yadav

2 Answers

0
votes

Should work if you wrap it to AutoSchema

schema = AutoSchema(manual_fields=[
    coreapi.Field(
        "data",
        required=True,
        location="body",
        description='{"items":[], "name":str}',
        schema=coreschema.Object()
    ),
])
0
votes

it is been a long time from this question but if someone needs, this is your solution, you need to set location to 'form', so your schema would be like this one:

manual_fields=[
    coreapi.Field(
        "data",
        required=True,
        location="form",
        description='{"items":[], "name":str}',
        schema=coreschema.Object()
    ),
])

For more instructions you can see Schemas - Django Rest Framework

Here I extract the section you need:

LOCATION

Determines how the information is encoded into the request. Should be one of the following strings:

"path"

Included in a templated URI. For example a url value of /products/{product_code}/ could be used together with a "path" field, to handle API inputs in a URL path such as /products/slim-fit-jeans/.

These fields will normally correspond with named arguments in the project URL conf.

"query"

Included as a URL query parameter. For example ?search=sale. Typically for GET requests.

These fields will normally correspond with pagination and filtering controls on a view.

"form"

Included in the request body, as a single item of a JSON object or HTML form. For example {"colour": "blue", ...}. Typically for POST, PUT and PATCH requests. Multiple "form" fields may be included on a single link.

These fields will normally correspond with serializer fields on a view.

"body"

Included as the complete request body. Typically for POST, PUT and PATCH requests. No more than one "body" field may exist on a link. May not be used together with "form" fields.

These fields will normally correspond with views that use ListSerializer to validate the request input, or with file upload views.