We want to create an endpoint for the root of our API to make it easier to browse the API with the browsable API feature and understand how everything works. Add the following code to the views.py
file to declare the ApiRoot
class. The code file for the sample is included in the restful_python_chapter_02_03
folder.
class ApiRoot(generics.GenericAPIView): name = 'api-root' def get(self, request, *args, **kwargs): return Response({ 'players': reverse(PlayerList.name, request=request), 'game-categories': reverse(GameCategoryList.name, request=request), 'games': reverse(GameList.name, request=request), 'scores': reverse(PlayerScoreList.name, request=request) })
The ApiRoot
class is a subclass of the rest_framework.generics.GenericAPIView
class and declares the get method. The GenericAPIView
class is the base class for all the other generic views. The ApiRoot
class defines the...