Mad*_*bat 14 python django django-rest-framework
我有一个带有外键的模型的序列化器.要求是在创建时,可以将外键设置为相关模型中的任何现有对象,但是在更新时,不能更改相关对象.我可以在自定义中检查这个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
Run Code Online (Sandbox Code Playgroud)
tre*_*zko 21
您绝对可以使用字段上的验证来执行此操作.您检查是否是更新与创建的方式是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
Run Code Online (Sandbox Code Playgroud)
另一种方法是,如果您正在更新,则覆盖字段是read_only.这可以__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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7332 次 |
| 最近记录: |