python - Displaying both ViewSets and GenericAPIViews in the Django REST framework API Root documentation -
this question has answer here:
view:
class fooviewset(viewsets.readonlymodelviewset): queryset = foo.objects.filter(state=2) serializer_class = fooserializer class barviewset(viewsets.readonlymodelviewset): queryset = bar.objects.filter(state=2) serializer_class = barserializer class bazviewlist(generics.listapiview): queryset = baz.objects.all() authentication_classes = (tokenauthentication,) permission_classes = (isauthenticated,) serializer_class = bazserializer def list(self, request): queryset = baz.objects.all() serializer = bazserializer(queryset, many=true) return response(serializer.data)
urls:
from django.conf.urls import include django.conf.urls.defaults import patterns, url rest_framework import routers rest_framework.authtoken import views vs router = routers.defaultrouter() router.register(r'foo', views.fooviewset) router.register(r'bar', views.barviewset) #router.register(r'baz', views.bazviewlist) urlpatterns = patterns('app.restapi.views', url(r'^v1/baz/', bazviewlist.as_view(), name='baz_view_list'), url(r'^v1/', include(router.urls)), url(r'^v1/api-token-auth/', vs.obtain_auth_token))
now, way if browse v1/ endpoint (which should provide documentation), api root lists foo , bar, not baz:
api root /api/rest/v1/ http 200 ok content-type: application/json vary: accept allow: get, head, options { "foo": "http://localhost:8000/api/rest/v1/foo/", "bar": "http://localhost:8000/api/rest/v1/bar/" }
the easiest way fix change bazviewlist
bazviewset
inherits rest_framework.mixins.listmodelmixin
, rest_framework.viewsets.genericviewset
:
from rest_framework import viewsets, mixins class bazviewset(mixins.listmodelmixin, viewsets.genericviewset): ...
this set internal plumbing bazviewset can registered router , magic of router take care of urls you.
Comments
Post a Comment