This time, we will write our API views by declaring class-based views, instead of function-based views. We might code classes that inherit from the rest_framework.views.APIView
class and declare methods with the same names than the HTTP verbs we want to process: get
, post
, put
, patch
, delete
, and so on. These methods receive a request
argument as happened with the functions that we created for the views. However, this approach would require us to write a lot of code. Instead, we can take advantage of a set of generic views that we can use as our base classes for our class-based views to reduce the required code to the minimum and take advantage of the behavior that has been generalized in Django REST Framework.
We will create subclasses of the two following generic class views declared in rest_framework.generics
:
ListCreateAPIView
: Implements theget
method that retrieves a listing of a queryset and thepost
method that creates a model...