我有兩個ModelField(Char和Decimal),使用clean_formfield()方法分別清理它們會很簡單。但是,爲了顯示這兩個表單域,我想只顯示一個輸入字段與ChoiceField的組合,用戶可以決定下一個輸入是某些單詞還是數字。分割是必要的,因爲我希望稍後與數字進行彙總。後Formfield清理 - 清理Formfield中的子類
很明顯,我不能使用clean_formfield()因爲依賴性,所以它必須進入窗體清理器。問題是如何實現「後期清潔」?
class Input(models.Model):
...
number = models.DecimalField()
word = models.CharField()
...
class InputForm(forms.ModelForm):
...
type = forms.CharField(widget=forms.RadioSelect(choices=(('word','Word'),('number','Number'),)))
input = forms.CharField()
...
def clean(self):
cleaned_data = self.cleaned_data
type = cleaned_data.get('type')
input = cleaned_data.get('input')
if type == 'number':
<<<clean_Decimal_Formfield>>>
cleaned_data['number'] = input
cleaned_data['word'] = None
else:
<<<clean_Char_Formfield>>>
cleaned_data['number'] = None
cleaned_data['word'] = input
return cleaned_data
好,但現在你缺少現場驗證!輸入將包含或有效的charfield或某個有效的小數域! – 2011-03-25 13:28:32