DRF mixins 的常用方法
DRF mixins 常用的操作
-
GenericAPIView
-
继承自APIVIew,增加了对于列表视图和详情视图可能用到的通用支持方法,queryset为列表视图的查询集,serializer_class为视图使用的序列化器
-
代码块
class TeacherView(ListModelMixin,GenericAPIView): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer
-
-
ListModelMixin
-
列表视图扩展类,提供list(request, *args, ** kwargs)方法快速实现列表视图,返回200状态码,该Mixin的list方法会对数据进行过滤和分页
-
serializers.py
# 老师类的序列化 class TeacherSerializer(serializers.ModelSerializer): class Meta: model = models.Teacher fields = '__all__'
-
views.py
class TeacherView(ListModelMixin,GenericAPIView): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer def get(self,request,*args,**kwargs): return self.list(request,*args,**kwargs)
-
urls.py
path('teacher/',views.TeacherView)
-
如图
-
-
UpdateModelMixin
-
更新视图扩展类,提供update(request, *args, **kwargs)方法,可以快速实现更新一个存在的数据对象。
同时也提供partial_update(request, *args, **kwargs)方法,可以实现局部更新。
成功返回200,序列化器校验数据失败时,返回400错误。 -
serializers.py
# 老师类的序列化 class TeacherSerializer(serializers.ModelSerializer): name = serializers.CharField(max_length=32) class Meta: model = models.Teacher fields = '__all__'
-
views.py
class TeacherOtherView(GenericAPIView,UpdateModelMixin): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer def put(request,*args,**kwargs): return self.update(request,*args,**kwargs)
-
urls.py
re_path(r'teacher/(?P<pk>\d+)/$',views.TeacherOtherView.as_view())
-
如图
-
-
DestroyModelMixin
-
删除视图扩展类,提供destroy(request, *args, **kwargs)方法,可以快速实现删除一个存在的数据对象。成功返回204,不存在返回404
-
serializers.py
class TeacherSerializer(serializers.ModelSerializer): class Meta: model = models.Teacher fields = '__all__'
-
views.py
class TeacherOtherView(GenericAPIView,DestroyModelMixin): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer def delete(request,*args,**kwargs): return self.destroy(request,*args,**kwargs)
-
urls.py
re_path(r'teacher/(?P<pk>\d+)/$',views.TeacherOtherView.as_view())
-
如图
-
-
RetrieveModelMixin
-
详情视图扩展类,提供retrieve(request, *args, **kwargs)方法,可以快速实现返回一个存在的数据对象。如果存在,返回200, 否则返回404
-
serializers.py
class TeacherSerializer(serializers.ModelSerializer): class Meta: model = models.Teacher fields = '__all__'
-
views.py
class TeacherOtherView(GenericAPIView,RetrieveModelMixin): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer def get(self,request,*args,**kwargs): return self.retrieve(request,*args,**kwargs)
-
urls.py
re_path(r'teacher/(?P<pk>\d+)/$',views.TeacherOtherView.as_view())
- 如图
- 如图
-
-
CreateModelMixin
-
创建视图扩展类,提供create(request, *args, **kwargs)方法快速实现创建资源的视图,成功返回201状态码。如果序列化器对前端发送的数据验证失败,返回400错误。
-
serializers.py
# 老师类的序列化 class TeacherSerializer(serializers.ModelSerializer): name = serializers.CharField(max_length=32) class Meta: model = models.Teacher fields = '__all__'
-
views.py
class TeacherView(GenericAPIView,CreateModelMixin): queryset = models.Teacher.objects.all() serializer_class = TeacherSerializer # 添加数据 def post(self,request,*args,**kwargs): return self.create(request,*args,**kwargs)
-
urls.py
path('teacher/',views.TeacherView.as_view())
-
如图
-