Building list and detail views

REST framework comes with a set of generic views and mixins that you can use to build your API views. These provide functionality to retrieve, create, update, or delete model objects. You can see all generic mixins and views provided by REST framework at https://www.django-rest-framework.org/api-guide/generic-views/.

Let's create list and detail views to retrieve Subject objects. Create a new file inside the courses/api/ directory and name it views.py. Add the following code to it:

from rest_framework import genericsfrom ..models import Subjectfrom .serializers import SubjectSerializerclass SubjectListView(generics.ListAPIView):    queryset = Subject.objects.all()    serializer_class = SubjectSerializerclass SubjectDetailView(generics.RetrieveAPIView): ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.