对于以下内容,我使用的是 Python 2.7/Django 1.5。
我正在尝试重构一些使用自定义验证程序覆盖 Django 表单的 clean() 方法的生产代码。它在多个表单中重复出现,因此我们希望将其抽象为一个外部函数,我们可以在表单中调用它。
假设我有以下内容,
# ... imports ...
class MyProperties(models.Model):
label = models.CharField(max_length=100, blank=True, null=True, verbose_name=u'label'
def __unicode__(self):
return self.label
class MyForm(forms.ModelForm):
choices = forms.ModelChoiceField(required=False, ...)
class Meta:
model = MyProperties
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
def clean(self):
return my_custom_clean_function(self)
def my_custom_clean_function(form_instance):
cleaned_data = super(type(form_instance), form_instance).clean() ## DOES NOT WORK
# other validation routines
return cleaned_data
从类外部调用 my_custom_clean_function 的 super 会导致超过最大递归深度(只是调用 form_instance 类中的 clean() 方法,调用自定义函数等...
创建 ModelForm 的临时实例并调用它的 clean 方法似乎不起作用,因为它没有任何字段。
有什么方法可以按惯用方式执行此操作,还是我最好从表单类内部调用父 clean method(),然后将该数据传递给我的自定义方法以进行进一步验证?
最佳答案
你的 super(type(instance), instance).clean()
也可以;除非你进一步子类化你的表单,此时 type(instance)
将是错误的传递对象。
或者在方法中使用 super()
并将结果传递给:
class MyForm(forms.ModelForm):
# ...
def clean(self):
return my_custom_clean_function(super(MyForm, form_instance).clean())
def my_custom_clean_function(cleaned_data):
# other validation routines
return cleaned_data
或者你可以让它成为一个混入:
class SharedCleanupMixin:
def clean(self):
cleaned_data = super(SharedCleanupMixin, self).clean()
# other validation routines
return cleaned_data
并在您的表单中使用它:
class MyForm(forms.ModelForm, SharedCleanupMixin):
# ...
关于python - 从 Django 的类外部使用 super() (或替代它,如果有的话),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31170141/