`TypeError` when calling create(). You may need to make the field read-only, or override the create() method

前端 未结 1 1702
情书的邮戳
情书的邮戳 2021-01-04 20:26

Not sure what\'s going on here. I\'m trying to perform create a new instance via Django-rest-framework. What am I doing wrong?

There are a few read-

相关标签:
1条回答
  • 2021-01-04 21:23

    Maybe try something like this in your serializer,

    class ClassificationLabelListSerializer(serializers.ModelSerializer):
        lastChanged = serializers.DateTimeField(read_only=True)
        identifier = serializers.CharField(read_only=True)
        revision = serializers.IntegerField(read_only=True)
        class Meta:
            model = ClassificationLabel
            fields = ('displayName', 'helpText', 'identifier', 'backgroundColour', 'foregroundColour', 'comment', 'description', 'lastChanged', 'revision')
    
        def create(self, validated_data):
            return ClassificationLabel.objects.create(**validated_data)
    

    Also, edit the save method in your models.py,

    def save(self, *args, **kwargs):
        self.revision += 1
        return super(ClassificationLabel, self).save(*args, **kwargs) #
    
    0 讨论(0)
提交回复
热议问题