https://github.com/encode/django-rest-framework/issues/1682 https://stackoverflow.com/questions/36327029/model-with-this-field-already-exist-on-put-call-django-rest-framework/36334825
model:
Serializer: view:
用Django rest framework POST的时候出现错误,不能添加已经存在的外键。
the unique validator of the name field created by the ModelSerializer. So the solution is to remove the validator ModelSerializer 创建的 name 字段的唯一验证器。 所以解决方案是通过以下方式删除验证器
class equipmentSerializer(serializers.ModelSerializer):
class Meta:
model = equipment
fields = '__all__'
read_only_fields = [
"time",
]
extra_kwargs = {
'device_id': {'validators': []},
}
增加extra_kwargs 将主键的验证设为空
|