为什么django的model.save()调用full_clean()? [英] Why doesn't django's model.save() call full_clean()?

查看:817
本文介绍了为什么django的model.save()调用full_clean()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人知道有没有什么好的理由,为什么django的orm不会在模型中调用full_clean,除非它被保存为模型窗体的一部分,我才好奇。请注意,当您调用模型的save()方法时,将不会自动调用full_clean()。当您想为自己手动创建的模型运行一步模型验证时,您需要手动调用它。
django的完整文档


(注意:Django 1.6的报价已更新...以前的django文档也有关于ModelForms的警告。 / p>

有没有很好的理由,人们不会想要这种行为?我想,如果你花时间为模型添加验证,那么每次保存模型时,都希望验证运行。



我知道如何获得一切正常,我只是在寻找一个解释。

解决方案

AFAIK,这是因为向后兼容。还有与排除字段的ModelForms,默认值的模型,pre_save()信号等)的ModelForms有问题。



您可能会引用的来源:




I'm just curious if anyone knows if there's good reason why django's orm doesn't call 'full_clean' on a model unless it is being saved as part of a model form.

Note that full_clean() will not be called automatically when you call your model’s save() method. You’ll need to call it manually when you want to run one-step model validation for your own manually created models. django's full clean doc

(NOTE: quote updated for Django 1.6... previous django docs had a caveat about ModelForms as well.)

Are there good reasons why people wouldn't want this behavior? I'd think if you took the time to add validation to a model, you'd want that validation run every time the model is saved.

I know how to get everything to work properly, I'm just looking for an explanation.

解决方案

AFAIK, this is because of backwards compatibility. There are also problems with ModelForms with excluded fields, models with default values, pre_save() signals, etc.

Sources you might be intrested in:

这篇关于为什么django的model.save()调用full_clean()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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