7

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.

Kreychek
  • 717
  • 7
  • 16
  • 1
    Is it possible to follow the [approach in Tutorial 5](http://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis#creating-an-endpoint-for-the-root-of-our-api) and define an explicit root endpoint with the `api_view` decorator? – Neil Jun 21 '14 at 09:38
  • found another question that helped me: http://stackoverflow.com/questions/30389248/how-can-i-register-a-single-view-not-a-viewset-on-my-router?rq=1 – Gavin Palmer Mar 06 '17 at 16:53

1 Answers1

3

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.
Denis Cornehl
  • 4,104
  • 1
  • 20
  • 24