logo       

overriding clean method on ModelForm class: msg#02218

DjangoUsers

Subject: overriding clean method on ModelForm class


Hey,

I had a problem recently that caused me some confusion!

I have a model that has a OneToOne field as the primary key. When
adding a new record in the admin form and entering a key that already
existed, I wasn't getting the expected "record already exists" error,
instead it was just overwriting the existing record!!

The problem was because I was implementing my own "clean" method on
the AdminForm. e.g.

class MyModelAdminForm(forms.ModelForm):
def clean(self):
....
return self.cleaned_data

as per the doco:
http://docs.djangoproject.com/en/1.0/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

But this then ignores the standard form validation that should still
occur. By adding a call to the super class clean method everything
became fine!

class MyModelAdminForm(forms.ModelForm):
def clean(self):
self.cleaned_data = super(MyModelAdminForm, self).clean()
....
return self.cleaned_data

Just be aware that any invalid data will be removed from cleaned_data

This is really a bug in the documentation I think... Can the doco be
updated??

Regards,
Simon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to
django-users+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | Mail Home | sitemap | FAQ | advertise