Mixins类的实际用途是什么?我实在不明白。所有 mixins 类(如 CreateModelmixin、Listmixin 等功能)都已在基于类的视图(如 ListCreateApiView)中可用。
For eg:
class ExampleView(ListCreateAPIView
DestroyAPIView,
RetrieveUpdateAPIView):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
pagination_class = CustomPageNumberPagination
使用 mixins 我们可以通过以下方式做到这一点:
class ExampleView(ListAPIView,
mixins.CreateModelMixin):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
pagination_class = CustomPageNumberPagination
当我检查时https://www.cdrf.co/ https://www.cdrf.co/我看到 CreateModelMishing 中可用的方法如下:
def create(self, request, *args, **kwargs):
def get_success_headers(self, data):
def perform_create(self, serializer):
这些方法在ListCreateApiView中已经有了,那为什么Django还要去创建这个无用的类呢?