Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get APIViews and ViewSets to show on API Root when using Router with Django REST Framework?

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.

like image 346
Kreychek Avatar asked Jun 01 '14 22:06

Kreychek


1 Answers

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.
like image 194
Denis Cornehl Avatar answered Sep 17 '22 21:09

Denis Cornehl