0
我在自定義inlineformset中遇到問題。我想驗證跳過具有delete checked
的行。在驗證中跳過刪除行
class TravelsRelationsForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TravelsRelationsForm, self).__init__(*args, **kwargs)
if kwargs.has_key('instance'):
instance = kwargs['instance']
else:
try:
self.initial['title']
except:
self.initial[DELETION_FIELD_NAME] = True
# Force update of child
def has_changed(self, *args, **kwargs):
return True
def clean(self):
cleaned_data = self.cleaned_data
count = 0
for form in self.forms:
try:
if not form.cleaned_data['DELETE']:
count += 1
except AttributeError:
pass
if count < 1:
raise forms.ValidationError('You must have at least one title')
return cleaned_data
同時我wan't執行至少一行。這不起作用,因爲TravelsRelationsForm' object has no attribute 'forms'
。
謝謝!
由於某種原因,這不起作用...我仍然得到所有標記爲缺失的空行(刪除選中) – Mission 2012-03-13 08:53:32
其實問題是Django返回所有字段,即使它們不是必需的......他們可以是空的,但它們存在於travelformset.errors中... – Mission 2012-03-13 09:03:47