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

查看:60
本文介绍了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:


ModelSerializer验证与ModelForm之间的差异。



此更改还意味着我们不再在模型实例上使用 .full_clean()方法
,而是对$ b $显式执行所有验证b串行器。这样可以提供更清晰的分隔,并确保
在ModelSerializer类
上没有自动验证行为,而该行为也不能轻易在常规Serializer类上复制。

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序列化器,您可能会被困在这里,因为在保存主模型之前您将无法创建嵌套模型,这将使​​完整调用更加混乱—一些对象将被创建,而其他对象将被创建。不。很难确定何时以及使用他们的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天全站免登陆