为什么django 的model.save() 不调用full_clean()? [英] Why doesn't django's model.save() call full_clean()?
问题描述
我只是好奇是否有人知道 django 的 orm 是否有充分的理由不对模型调用full_clean",除非它被保存为模型表单的一部分.
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.
请注意,当您调用模型的 save() 方法时,full_clean() 不会自动调用.当您想为自己手动创建的模型运行一步模型验证时,您需要手动调用它.django 的完整清洁文档
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
(注意:为 Django 1.6 更新了报价...之前的 django 文档也有关于 ModelForms 的警告.)
(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,这是因为向后兼容.带有排除字段的 ModelForms、带有默认值的模型、pre_save() 信号等也存在问题.
AFAIK, this is because of backwards compatibility. There are also problems with ModelForms with excluded fields, models with default values, pre_save() signals, etc.
您可能感兴趣的来源:
- http://code.djangoproject.com/ticket/13100
- http://groups.google.com/group/django-开发人员/browse_frm/thread/b888734b05878f87
这篇关于为什么django 的model.save() 不调用full_clean()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!