我有一个序列化程序类:
class AdministratorCreateUpdateSerializer(ModelSerializer):
class Meta:
model = Administrator
fields = [
'username',
'email',
'password',
'first_name',
'last_name',
]
username = serializers.CharField(
source='user.username',
validators=[UniqueValidator(queryset=User.objects.all())]
)
email = serializers.EmailField(
source='user.email',
validators=[UniqueValidator(queryset=User.objects.all())]
)
password = serializers.CharField(
source='user.password',
allow_blank=True,
style={'input_type': 'password'}
)
first_name = serializers.CharField(
source='user.first_name'
)
last_name = serializers.CharField(
source='user.last_name'
)
当我创建新管理员时,用户名和电子邮件的验证器运行良好。
但是当我更新数据时。我只是简单地填充旧数据并保存,但验证器说用户名和电子邮件必须是唯一的。
如何更新此验证程序仅在使用不等于旧值的新值进行更新时执行?
答案 0 :(得分:1)
之前我遇到过同样的问题。我通过覆盖序列化器上的update()
方法并使用Django queryset的exclude()
方法来修复它:
def update(self, instance, validated_data):
username = validated_data.get('username', '')
if User.objects.exclude(pk=instance.pk).filter(username=username):
raise serializers.ValidationError('User with this username already exists.')
instance.__dict__.update(**validated_data)
instance.save()
return instance
现在它只会在任何其他用户尝试更新其用户名时引发错误,这与您的用户名相同。
希望这会有所帮助:)
更新:
您可以覆盖validate_<property>()
方法。在你的情况下:
def validate_username(self, value):
# self.instance is the current instance (Administrator)
# this method is called on every request, so you should do
# if self.instance
# to check if the method is update/post, so you can have an instance
# and then you do the check for the username uniqueness
if self.instance and User.objects.exclude(pk=self.instance.pk).filter(username=value):
raise serializers.ValidationError('User with this username already exists.')
return value