This is my views.py:
class ChoicesViewSet(viewsets.ModelViewSet):
queryset = SingleChoice.objects.all()
serializer_class = SingleChoiceSerializer
...
class AssessmentTakersViewSet(viewsets.ModelViewSet):
queryset = AssessmentTaker.objects.all()
serializer_class = AssessmentTakersSerializer
...
@api_view(['POST'])
@parser_classes((JSONParser,))
def studio_create_view(request, format=None):
""""
A view that accept POST request with JSON content and in turn build out the
questions and choices. Post with application/json type.
"""
...
This is my urls.py:
urlpatterns = [
# http://localhost:8000/survey/api/studio-create
path('api/studio-create', views.studio_create_view, name='studio-create-api'),
]
# drf config
router = routers.DefaultRouter()
router.register('api/choices', views.ChoicesViewSet)
router.register('api/assessment-takers', views.AssessmentTakersViewSet)
urlpatterns += router.urls
This works functionally, and is considered feature-complete, but but because studio-create_view is not registered with the router, the path doesn't show up under the API Root which isn't great documentation wise. In other words, the API root from Django Rest Framework isn't aware of the path, and a GET request to the root would list only:
http://localhost:8000/survey/
HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"api/choices": "http://localhost:8000/survey/api/choices/",
"api/assessment-takers": "http://localhost:8000/survey/api/assessment-takers/",
...
}
My question is, outside of viewsets that inherit from ModelViewSet, how do we get the custom views like studio_create_view to be registered with the router that Django Rest Framework provide so it's listed under the API root?
One would be inclined to try something like:
router.register('api/studio-create', views.studio_create_view, basename='studio-create-api')
But this will not work, throwing the following exception:
AttributeError: 'function' object has no attribute 'get_extra_actions'
Omitting the name or basename also doesn't work.
router.register('api/studio-create', views.studio_create_view)
This would prompt Django Rest Framework to throw the following exception:
AssertionError:
basenameargument not specified, and could not automatically determine the name from the viewset, as it does not have a.querysetattribute.
Note that the custom view function studio_create_view is a function, not a class, and hence doesn't inherit from any other classes, so similar questions where the view inherits from viewsets.ViewSet or viewsets.ModelViewSet or any of the likes are unlikely to be helpful. Appreciate any pointers as I've scoured the documentation extensively to no avail.
