We want to be able to list all the users and retrieve the details for a single user. We will create subclasses of the two following generic class views declared in rest_framework.generics
:
ListAPIView
: Implements the get
method that retrieves a listing of a querysetRetrieveAPIView
: Implements the get
method to retrieve a model instanceGo to the gamesapi/games
folder and open the views.py
file. Add the following code after the last line that declares the imports, before the declaration of the GameCategoryList
class. The code file for the sample is included in the restful_python_chapter_03_04
folder:
from django.contrib.auth.models import User from games.serializers import UserSerializer from rest_framework import permissions from games.permissions import IsOwnerOrReadOnly class UserList(generics.ListAPIView): queryset = User.objects.all() serializer_class = UserSerializer name = 'user-list' class UserDetail(generics.RetrieveAPIView): queryset = User.objects.all() serializer_class = UserSerializer name = 'user-detail'
Add the following highlighted lines to the ApiRoot class declared in the views.py
file. Now, we will be able to navigate to the user-related views throughout the browsable API. The code file for the sample is included in the restful_python_chapter_03_04
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),
'users': reverse(UserList.name, request=request),
})
Go to the gamesapi/games
folder and open the urls.py
file. Add the following elements to the urlpatterns
string list. The new strings define the URL patterns that specify the regular expressions that have to be matched in the request to run a specific method for the previously created class based-views in the views.py
file: UserList
and UserDetail
. The code file for the sample is included in the restful_python_chapter_03_04
folder:
url(r'^users/$', views.UserList.as_view(), name=views.UserList.name), url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view(), name=views.UserDetail.name),
We have to add a line in the urls.py
file in the gamesapi
folder, specifically, the gamesapi/urls.py
file. The file defines the root URL configurations and we want to include the URL patterns to allow the browsable API to display the login and logout views. The following lines show the new code for the gamesapi/urls.py
file. The new line is highlighted. The code file for the sample is included in the restful_python_chapter_03_04
folder:
from django.conf.urls import url, include
urlpatterns = [
url(r'^', include('games.urls')),
url(r'^api-auth/', include('rest_framework.urls'))
]
We have to make changes to the GameList
class-based view. We will override the perform_create
method to populate the owner
before a new Game
instance is persisted in the database. The following lines show the new code for the GameList
class in the views.py
file. The new lines are highlighted. The code file for the sample is included in the restful_python_chapter_03_04
folder:
class GameList(generics.ListCreateAPIView):
queryset = Game.objects.all()
serializer_class = GameSerializer
name = 'game-list'
def perform_create(self, serializer):
# Pass an additional owner field to the create method
# To Set the owner to the user received in the request
serializer.save(owner=self.request.user)
The GameList
class inherits the perform_create
method from the rest_framework.mixins.CreateModelMixin
class. Remember that the generics.ListCreateAPIView
class inherits from CreateModelMixin
class and other classes. The code for the overridden perform_create
method passes an additional owner
field to the create method by setting a value for the owner
argument for the call to the serializer.save
method. The code sets the owner attribute to the value of self.request.user
, that is, to the user associated to the request. This way, whenever a new game is persisted, it will save the user associated to the request as its owner.
18.191.189.23