I’m having problem on a custom inlineformset. I want validation to skip rows which have 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
At the same time I wan’t to enforce at least one row. This doesn’t work, because TravelsRelationsForm' object has no attribute 'forms'.
Thanks!
It is normal that you see this error. You have inlineformset logic in the modelform. You could replace your modelform clean() method with a full_clean() method:
And such a formset validation:
Could be done directly in the view.