[Fixed]-Mix View and ViewSet in a browsable api_root


You can define your views as ViewSets with only one method.
So you can register it in router and it will be in one space with ViewSets.



Doesn’t look like there’s a simple way to do that using the DefaultRouter, you’d have to build your own router. If it’s any consolation the DefaultRouter’s logic for generating the APIRoot view is fairly simple and you could probably easily roll your own, similar router based on the DefaultRouter class (e.g. Modify the ApiRoot class implementation to fetch additional URLs to include, you can do this any number of ways e.g. pass them into your Router’s constructor):



From http://www.django-rest-framework.org/api-guide/viewsets/:

A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as .get() or .post(), and instead provides actions such as .list() and .create()

Which means we can extend your ViewSets:

def other_rooms_view(request):
    return Response(...)

class RoomsViewSet(ViewSet):

    def list(self, request):
        return other_rooms_view(request)

restaurant_router = DefaultRouter()
restaurant_router.register(r'rooms', RoomsViewSet)

Leave a comment