If I have a view like:
class MyAPIView(APIView):
def get(self, request, name=None):
return {"hello": name or "world"}
How can I get that included in the generated documentation? Specifically, how can I get it included in the API Root, so it appears when I visit "http://example.com/api/"?
The documentation includes an example of an APIView with description, but doesn't describe the process of actually getting it included in the API browser.
Updated version of @imyousuf code to work with DRF 3.4.1.
How to use:
Solution by @imyousuf is nice, but it doesn't support url namespaces and is a bit outdated.
Here's an update of it:
I have optimized HybridRouter for my use case and removed some code. Check it out:
And usage:
Or:
To mix with routers and APIView classes or method based in such a way that the API Root displays both with minimal code views in the APIRoot view I wrote a custom router that extends DefaultRouter and overrides get_urls and get_api_root_view; it looks like as follows :
Then I use it as -
Hi David, first up I wouldn't quite describe the browsable API as 'generated documentation'.
If you need static documentation you're best off looking at a third party tool like django-rest-swagger.
The browsable API does mean that the APIs you build will be self-describing, but it's a little different from conventional static documentation tools. The browsable API ensures that all the endpoints you create in your API are able to respond both with machine readable (ie JSON) and human readable (ie HTML) representations. It also ensures you can fully interact directly through the browser - any URL that you would normally interact with using a programmatic client will also be capable of responding with a browser friendly view onto the API.
Just add a docstring to the view and it'll be included in the browsable API representation of whichever URLs you route to that view.
By default you can use markdown notation to include HTML markup in the description but you can also customise that behaviour, for example if you'd rather use rst.
You'll just want to explicitly add the URL to into the response returned by whatever view you have wired up to
/api/
. For example...