Django - BaseSerializer.is_valid() 缺少 1 个必需的位置参数:'self'
Django - BaseSerializer.is_valid() missing 1 required positional argument: 'self'
我正在创建一个基本视图集 class 并尝试在两个 class 中使用它。
视图集有一个创建方法,它使用 get_serializer_class() 来获取序列化器并创建对象。
但是 is_valid() 抛出错误
TypeError: BaseSerializer.is_valid() missing 1 required positional argument: 'self'
这里是基地 class:
class BaseViewSet(BaseFilterViewSet):
permission_classes = [IsAuthenticated]
def create(self, request: Request, *args: Any, **kwargs: Any) -> Response:
context = super(BaseViewSet, self).get_serializer_context()
context.update({"request": request})
# serializer = super(BaseViewSet, self).get_serializer_class()
serializer = self.get_serializer_class()
serializer(data=request.data, context=context)
serializer.is_valid(raise_exception=True)
serializer.save(created_by=request.user)
return Response(serializer.data, status=HTTP_201_CREATED)
这是使用此视图集并定义 get_serializer_class 方法的子 class。
class ChildViewSet(BaseViewSet):
queryset = Child.objects.all().select_related("model2")
permission_classes = [IsAuthenticated]
def get_serializer_class(self) -> Type[BaseSerializer[_MT_co]]:
if self.action == "stock":
return StockSerializer
else:
return ChildSerializer
非常感谢您的投入和帮助。
您可以使用已包含请求的 .get_serializer(…)
[drf-doc]: this will automatically create the serializer with the serializer class that is specified in the .get_serializer_class()
[drf-doc] with the context retrieved by the .get_serializer_context(…)
method call [drf-doc](以及 view
和 format
)。因此,您可以将其实现为:
class BaseViewSet(BaseFilterViewSet):
permission_classes = [IsAuthenticated]
def create(self, request: Request, *args: Any, **kwargs: Any) -> Response:
serializer = self.<strong>get_serializer(</strong>data=request.data<strong>)</strong>
serializer.is_valid(raise_exception=True)
serializer.save(created_by=request.user)
return Response(serializer.data, status=HTTP_201_CREATED)
我正在创建一个基本视图集 class 并尝试在两个 class 中使用它。 视图集有一个创建方法,它使用 get_serializer_class() 来获取序列化器并创建对象。 但是 is_valid() 抛出错误
TypeError: BaseSerializer.is_valid() missing 1 required positional argument: 'self'
这里是基地 class:
class BaseViewSet(BaseFilterViewSet):
permission_classes = [IsAuthenticated]
def create(self, request: Request, *args: Any, **kwargs: Any) -> Response:
context = super(BaseViewSet, self).get_serializer_context()
context.update({"request": request})
# serializer = super(BaseViewSet, self).get_serializer_class()
serializer = self.get_serializer_class()
serializer(data=request.data, context=context)
serializer.is_valid(raise_exception=True)
serializer.save(created_by=request.user)
return Response(serializer.data, status=HTTP_201_CREATED)
这是使用此视图集并定义 get_serializer_class 方法的子 class。
class ChildViewSet(BaseViewSet):
queryset = Child.objects.all().select_related("model2")
permission_classes = [IsAuthenticated]
def get_serializer_class(self) -> Type[BaseSerializer[_MT_co]]:
if self.action == "stock":
return StockSerializer
else:
return ChildSerializer
非常感谢您的投入和帮助。
您可以使用已包含请求的 .get_serializer(…)
[drf-doc]: this will automatically create the serializer with the serializer class that is specified in the .get_serializer_class()
[drf-doc] with the context retrieved by the .get_serializer_context(…)
method call [drf-doc](以及 view
和 format
)。因此,您可以将其实现为:
class BaseViewSet(BaseFilterViewSet):
permission_classes = [IsAuthenticated]
def create(self, request: Request, *args: Any, **kwargs: Any) -> Response:
serializer = self.<strong>get_serializer(</strong>data=request.data<strong>)</strong>
serializer.is_valid(raise_exception=True)
serializer.save(created_by=request.user)
return Response(serializer.data, status=HTTP_201_CREATED)