Mixin扩展类

特点:

1)继承自object

2)Mixin不能单独使用,需要配合GenericAPIView使用

5个扩展类
1)ListModelMixin:list

class ListModelMixin(object):
    def list(self, request, *args, **kwargs):
        """封装获取一组数据通用代码"""
        queryset = self.get_queryset()
        serializer = self.get_serializer(queryset, many=True)
        return Response(serializer.data)

2)CreateModelMixin:create

class CreateModelMixin(object):
    def create(self, request, *args, **kwargs):
        """封装新增数据通用代码"""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()  # 调用序列化器类中的create
        return Response(serializer.data, status=status.HTTP_201_CREATED)

Mixin扩展类

上一篇:Python 的 Mixin 类(转)


下一篇:python_mixin设计模式