如何在DRF中验证更新字段?

2024-06-01 06:56:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带外键的模型的序列化程序。要求是在创建时,可以将外键设置为相关模型中的任何现有对象,但在更新时,不能更改相关对象。我可以在自定义的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

Tags: name模型selfdatamodelsdefthisdo
1条回答
网友
1楼 · 发布于 2024-06-01 06:56:48

使用字段上的验证绝对可以做到这一点。检查它是更新还是创建的方法是检查验证函数中的self.instance。有人提到过这件事in the serializer documentation

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

另一种方法是,如果字段是只读的,则在更新时覆盖该字段。这可以在序列化程序的__init__中完成。与验证器类似,您只需查找一个实例,如果有数据:

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

相关问题 更多 >