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 theget
method that retrieves a listing of a querysetRetrieveAPIView
: Implements theget
method to retrieve a model instance
Go 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...