Django序列化器create方法获取空的验证数据

时间:2019-02-24 10:08:03

标签: django django-rest-framework serializer

我正在尝试创建一个新表。

我的序列化器:

class Animal{
    private $name;
    public function __construct($name) {
        $this->name = $name;
    }
    public function Greet(){
        echo "Hello, I'm some sort of animal and my name is ", $this->name ;
    }
}

 class Dog extends Animal{
     private $type;

     public function __construct($name,$type) {
         $this->type = $type;
           parent::__construct($name);

     }
     public function Greet(){
         echo "Hello, I'm a ", $this->type, " and my name is ", $this->name;
     }
 }
   $dog2 = new Dog('Jock','dog');
   $dog2->Greet();

视图集:

class PostSandBox(serializers.Serializer):
    def create(self, validated_data):
        print(validated_data)
        return Sheets.objects.create(**validated_data)

所以我的问题是class Sandbox(mixins.CreateModelMixin, viewsets.GenericViewSet): serializer_class = PostSandBox 始终为空。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

要使用Django模型,您需要使用serializers.ModelSerializer而不是serializers.Serializer