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

1👍

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.

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

-1👍

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):

https://github.com/tomchristie/django-rest-framework/blob/86470b7813d891890241149928d4679a3d2c92f6/rest_framework/routers.py#L263

-1👍

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)
👤knite

Leave a comment