2011-07-29 127 views
7

我想向ModelForm添加一個額外的字段。這似乎很容易,但我得到了以下錯誤:Django:向模型添加一個字段

Django Version: 1.4 pre-alpha SVN-16573 
Exception Type: TypeError 
Exception Value:  
argument of type 'NoneType' is not iterable 
Exception Location: /usr/local/lib/django-trunk/django/forms/models.py in construct_instance, line 39 
Python Executable: /usr/bin/python 
Python Version: 2.6.6 

這是代碼:

class NodeForm(forms.ModelForm): 
    password2 = forms.CharField(max_length=20, required=True, widget=forms.PasswordInput()) 

    class Meta: 
     model = Node 

    def __init__(self, *args, **kwargs): 
     super(NodeForm, self).__init__(*args, **kwargs) 
     # css classes for fields 
     for v in self.fields: 
      self.fields[v].widget.attrs['class'] = 'text ui-widget-content ui-corner-all' 

    def clean(self): 
     ''' Calls parent clean() and performs additional validation for the password field ''' 
     super(NodeForm, self).clean() 
     password = self.cleaned_data.get('password') 
     password2 = self.cleaned_data.get('password2') 
     # password and password2 must be the same 
     if password != password2: 
      raise forms.ValidationError('I due campi password non corrispondono.') 

我無法修復它。我究竟做錯了什麼?

+0

您正在使用從主幹(1.4)Django的,你能添加完整的堆棧跟蹤(誤差線在Django:'如果不f.editable或isinstance(f,models.AutoField)或不clean.data中的f.name) – christophe31

回答

8

應該返回從作爲記錄here 也就是說clean方法清理數據:

def clean(self): 
    # perform checks 
    return self.cleaned_data 
+0

我應該審閱文檔!謝謝。 – nemesisdesign

相關問題