Creating class-based views and using generic classes
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 the rest_framework.generics
module:
ListCreateAPIView
: Implements theget
method, which retrieves a listing of aqueryset
, and thepost
method, which...