【发布时间】:2010-01-29 17:27:13
【问题描述】:
我正在尝试解决我在this question 中概述的问题。目前看来我将不得不覆盖 ForeignKey 字段上的 to_python() 方法。但据我在 django 的源代码中看到的,ForeignKey 类实际上并没有声明 to_python() 方法,因此它必须从 Field 类继承它,这意味着它看起来像这样:
def to_python(self, value):
"""
Converts the input value into the expected Python data type, raising
django.core.exceptions.ValidationError if the data can't be converted.
Returns the converted value. Subclasses should override this.
"""
return value
只有这样是不对的……这意味着它没有抛出 ValidationError。然而,肯定有什么东西一定会抛出它......我的意思是,id 到对象的转换必须在某个地方发生,如果 id 不正确,肯定会抛出 ValidationError 吗?
或者也许正确的问题是在表单上的 clean_<fieldname>() 方法之前调用了哪些其他方法?我可以覆盖其中哪些?
【问题讨论】:
标签: django django-models django-forms