官术网_书友最值得收藏!

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 rest_framework.generics:

  • ListCreateAPIView: Implements the get method that retrieves a listing of a queryset and the post method that creates a model instance.
  • RetrieveUpdateDestroyAPIView: Implements the get, put, patch, and delete methods to retreive, completely update, partially update or delete a model instance.

Those two generic views are composed by combining reusable bits of behavior in Django REST Framework implemented as mixin classes declared in rest_framework.mixins. We can create a class that uses multiple inheritance and combine the features provided by many of these mixin classes. The following line shows the declaration of the ListCreateAPIView class as the composition of ListModelMixin, CreateModelMixin and rest_framework.generics.GenericAPIView:

class ListCreateAPIView(mixins.ListModelMixin, 
                        mixins.CreateModelMixin, 
                        GenericAPIView): 

The following line shows the declaration of the RetrieveUpdateDestroyAPIView class as the composition of RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin and rest_framework.generics.GenericAPIView:

class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin, 
                                   mixins.UpdateModelMixin, 
                                   mixins.DestroyModelMixin, 
                                   GenericAPIView): 

Now, we will create a Django class based views that will use the previously explained generic classes and the serializer classes to return JSON representations for each HTTP request that our API will handle. We will just have to specify a queryset that retrieves all the objects in the queryset attribute and the serializer class in the serializer_class attribute for each subclass that we declare. The generic classes will do the rest for us. In addition, we will declare a name attribute with the string name we will use to identify the view.

主站蜘蛛池模板: 濉溪县| 麻江县| 凭祥市| 临沧市| 雷州市| 尼玛县| 永顺县| 壶关县| 兴业县| 江都市| 潍坊市| 顺义区| 封开县| 朝阳市| 公安县| 浠水县| 纳雍县| 墨江| 苗栗市| 含山县| 通城县| 茌平县| 行唐县| 昂仁县| 彭山县| 津南区| 阳山县| 古交市| 高州市| 襄城县| 通化市| 合肥市| 枣阳市| 衡南县| 喀喇沁旗| 银川市| 凌海市| 辽源市| 京山县| 仲巴县| 额尔古纳市|