📜  `.create()` 方法默认不支持可写嵌套字段.为序列化程序 `room_api.serializers.roomSerializer` 编写显式的 `.create()` 方法,或在嵌套的序列化程序字段上设置 `read_only=True`. - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:59.251000             🧑  作者: Mango

代码示例1
class CoreInformation(generics.ListCreateAPIView):
    queryset = models.CoreInformation.objects.all()
    serializer_class = CustomCoreInfoSerializer

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        instance = self.perform_create(serializer)
        serializer = self.get_serializer(instance=instance)
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def perform_create(self, serializer):
        return serializer.create(validated_data=serializer.validated_data)