为什么 Django Rest Framework 不鼓励模型级验证? [英] Why does Django Rest Framework discourage model level validation?

查看:22
本文介绍了为什么 Django Rest Framework 不鼓励模型级验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django Rest Framework 序列化器在验证模型序列化器时不会调用 Model.clean.给出的解释是这会导致更清晰的关注点分离",来自 Django Rest Framework 3.0 发行说明:

Django Rest Framework serializers do not call the Model.clean when validating model serializers. The explanation given is that this leads to 'cleaner separation of concerns', from the Django Rest Framework 3.0 release notes:

这个变化也意味着我们不再使用 .full_clean() 方法在模型实例上,而是显式地执行所有验证序列化器.这提供了更清晰的分离,并确保ModelSerializer 类没有自动验证行为这也不能在常规序列化程序类上轻松复制.

Differences between ModelSerializer validation and ModelForm.

This change also means that we no longer use the .full_clean() method on model instances, but instead perform all validation explicitly on the serializer. This gives a cleaner separation, and ensures that there's no automatic validation behavior on ModelSerializer classes that can't also be easily replicated on regular Serializer classes.

但是 Django Rest Framework 的作者试图分离什么?

But what concerns are the authors of Django Rest Framework attempting to separate?

我的猜测是他们说模型实例不应该关心它自己的有效性.如果是这样的话,我不明白为什么.

My guess is that they're saying that a model instance should not be concerned about it's own validity. If that's the case I don't understand why.

推荐答案

模型的full_clean"有两个主要问题.第一个是技术性的.有几种情况根本不调用 full_clean.例如,当您执行 queryset.update() 时,您将绕过它.

There are two major issues with the model's "full_clean". The first one is technical. There are a couple of cases where the full_clean isn't called at all. For example, you'll bypass it when you do a queryset.update().

第二个是,如果您有一个复杂的业务逻辑 - 这通常是您拥有 full_clean 的原因 - 您很可能应该在业务逻辑中进行验证,而不是深入到模型中进行验证.每一层都应该负责自己的一致性,存储层——即模型——不应该关心业务层.

The second one is that if you have a complex business logic - which is usually why you'll have a full_clean - it's likely that you should do the validation in the business logic, not go down to the models to validate. Each layer should be responsible for its own consistency and the storage layer - ie models - shouldn't care about the business layer.

我能想到的另一件事是,一旦您拥有在序列化程序进行验证之后出现的模型,就会调用 full_clean.在这一点上,事情开始变得混乱,因为您有一个两步验证,其中创建了一个对象.

Another thing that I can think of is that full_clean will be called once you have a model that comes after the serializer has been doing its validation. At this point, things start getting messy because you have a two-step validation with an object created in between.

如果您正在使用 nested serializer,您可能会被困在这里,因为在保存主模型之前您将无法创建嵌套模型,这将使​​完全干净的调用更加混乱- 一些对象会被创建,其他的不会.很难弄清楚什么时候和什么对象应该用他们的 full_clean 进行验证,你可以肯定,当他们覆盖更新/清理并找出 full_clean 没有被调用时,用户会抱怨很多每个型号.这开始变得令人头疼,我们更愿意让事情更简单、更明确.

If you're using nested serializer, you might be stuck here because you won't be able to create nested models before the primary model has been saved which will make the full clean call even messier - some objects will be created, others will not. It's hard to figure out when and what object should be validated with their full_clean and you can be sure there'll be a lot of complaints from users when they'll override the update/clean and figure out the full_clean hasn't been called for every model. This started becoming a total headache and we prefer to keep things simpler and more explicit.

这篇关于为什么 Django Rest Framework 不鼓励模型级验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆