Python 如何在DRF中验证更新时的字段?
我有一个外键模型的序列化程序。要求是在创建时,可以将外键设置为相关模型中的任何现有对象,但在更新时,不能更改相关对象。我可以在自定义的Python 如何在DRF中验证更新时的字段?,python,django,django-rest-framework,Python,Django,Django Rest Framework,我有一个外键模型的序列化程序。要求是在创建时,可以将外键设置为相关模型中的任何现有对象,但在更新时,不能更改相关对象。我可以在自定义的update()中检查这一点,但是使用序列化程序验证来检查这一点会更优雅吗?但我不知道怎么做。示例代码: class Person(models.Model): name = models.CharField(max_length=256) spouse = models.ForeignKey(Person) class PersonSerial
update()
中检查这一点,但是使用序列化程序验证来检查这一点会更优雅吗?但我不知道怎么做。示例代码:
class Person(models.Model):
name = models.CharField(max_length=256)
spouse = models.ForeignKey(Person)
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
# this is how I know how to do this
def create(self, validated_data):
try:
spouse = Person.objects.get(pk=int(validated_data.pop('spouse')))
except Person.DoesNotExist:
raise ValidationError('Imaginary spouses not allowed!')
return Person.objects.create(spouse=spouse, **validation_data)
def update(self, person, validated_data):
if person.spouse.pk != int(validated_data['spouse']):
raise ValidationError('Till death do us part!')
person.name = validation_data.get('name', person.name)
person.save()
return person
# the way I want to do this
def validate_spouse(self, value):
# do validation magic
您完全可以使用字段上的验证来实现这一点。检查是更新还是创建的方法是在验证函数中检查
self.instance
。有人提到过这件事
self.instance
将保存现有对象及其值,因此您可以使用它来比较
我相信这对你的目的是有用的:
def validate_spouse(self, value):
if self.instance and value != self.instance.spouse:
raise serializers.ValidationError("Till death do us part!")
return value
另一种方法是在更新时重写字段是否为只读。这可以在序列化程序的
\uuuu init\uuu
中完成。与验证器类似,您只需查找一个实例,如果有数据:
def __init__(self, *args, **kwargs):
# Check if we're updating.
updating = "instance" in kwargs and "data" in kwargs
# Make sure the original initialization is done first.
super().__init__(*args, **kwargs)
# If we're updating, make the spouse field read only.
if updating:
self.fields['spouse'].read_only = True