Documenting your API — Django REST framework.
1 min readDec 3, 2020
Developing API with Django REST Framework is so easy. So I was looking for the simple way to quickly document API methods with short descriptions. What I found that it can be done incredibly simple.
You just document views by including docstrings. Describe each included method in docstring. For example:
class ListingViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet):
"""
General ViewSet description
create:
Create listing object with initial data.
retrieve:
Return the given listing.
update:
Update listing object.
partial_update:
Patch listing object.
destroy:
Delete listing object
"""
permission_classes = [IsAuthenticated]
serializer_class = ListingSerializer
queryset = Listings.objects.all()
Here is result (drf-yasg):
Read more here Built-in API documentation.
This approach is supported by Built-in API documentation, and also by drf-yasg.