Django Rest Framework: Disable field update after object is created

后端 未结 11 1387
独厮守ぢ
独厮守ぢ 2020-11-30 20:15

I\'m trying to make my User model RESTful via Django Rest Framework API calls, so that I can create users as well as update their profiles.

However, as I go through

11条回答
  •  北海茫月
    2020-11-30 20:38

    This post mentions four different ways to achieve this goal.

    This was the cleanest way I think: [collection must not be edited]

    class DocumentSerializer(serializers.ModelSerializer):
    
        def update(self, instance, validated_data):
            if 'collection' in validated_data:
                raise serializers.ValidationError({
                    'collection': 'You must not change this field.',
                })
    
            return super().update(instance, validated_data)
    

提交回复
热议问题