How to add documents to ViewSet

161 views
Skip to first unread message

秦斌

unread,
Jan 31, 2018, 9:09:09 AM1/31/18
to Django REST framework

I mean document is like this with visit http://127.0.0.1/docs/



this is my code about the document

class GoodsViewSet(viewsets.ReadOnlyModelViewSet):
    schema = ManualSchema(fields=[
        coreapi.Field(
            name="type_id",
            # ['name', 'required', 'location', 'schema', 'description', 'type', 'example']
            required=False,
            location="query",
            # schema=coreschema.Integer(minimum=1, maximum=20),
            description ="1 : fuirt  2 : seafood  3 : meat 4 : eggs 5 some else :  6 : freezen",
        ),
        coreapi.Field(
            "limit",
            required=False,
            location="query",
            schema=coreschema.Integer(minimum=1, maximum=20)
        ),
    ]
    )

I try to add description to coreapi.Field , but it does not works. and both 'type_id' and 'limit' are show in list and read. I just want to show them in list



I read the document of restful django. but i don't know  how to add document to ViewSet

I read flowing doc

all I got is not suitable to ViewSet. they are suitable for View.

Who can give me a example code to document a ViewSet. Thanks for help. Sorry for poor English.

Carlton Gibson

unread,
Feb 2, 2018, 2:37:40 AM2/2/18
to django-res...@googlegroups.com
Hi. 

This approach isn’t going to work with ViewSets. As you have seen, ManualSchema will be applied to each route, with no ability to scope per-action. (This is more a problem with ViewSets than anything else…)

Instead you need to subclass either ManualSchema or AutoSchema and override `get_link` (or for AutoSchema maybe `get_manual_fields`) to inspect `self.view.action`, and provide the appropriate fields. 

Something like: 

if self.view.action == ‘list’:
    # … create the fields for the list action…
else:
   # … create the fields for read action...

I hope that helps. 

Regards,

Carlton

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages