6
votes

I recently started using DRF and am wondering how to get the automatically generated API Root provided by a Router to display endpoint routes that don't come from ViewSet-based views.

In my case, I have some non-ViewSet (generic) class-based views (ListAPIView and RetrieveAPIView) in my views.py that I would like to appear in my API Root.

I am open to alternative solutions to this, e.g. should I abandon ViewSets and implement those endpoints differently, etc.

1
Is it possible to follow the approach in Tutorial 5 and define an explicit root endpoint with the api_view decorator?Neil
found another question that helped me: stackoverflow.com/questions/30389248/…Gavin Palmer

1 Answers

3
votes

I think the best way here is to:

  • keep using all your preferred ViewSets / Generic* / Mixins / bare views. This is where the real work happens.
  • rewrite the APIRoot-View (it is not that complex, see the source).
  • either use the urlconfig or extend the DefaultRouter to make DRF use your APIRoot-View.