Django Admin Form clean method override only working for one exception

2,992 views
Skip to first unread message

dean raemaekers

unread,
Jan 4, 2018, 7:27:44 AM1/4/18
to Django users
Hello,

I am trying to override the Forms in the Django Admin interface to have custom exceptions in the clean() method.

This works for my first validation, but it doesn't work for a second one in the same form, or on another form at all. I am wondering if I have the syntax wrong for multiple validations.

The code looks like this:

class ProviderForm(forms.ModelForm):
class Meta:
model = Provider
fields = '__all__'

def clean(self):
#provider start date and end date
provider_start_date = self.cleaned_data.get('provider_start_date')
provider_end_date = self.cleaned_data.get('provider_end_date')
if provider_start_date > provider_end_date:
raise forms.ValidationError("Start date can't be after end date")

#etqe_id and provider_code
provider_etqe_id = self.cleaned_data.get('provider_etqe_id')
provider_code = self.cleaned_data.get('provider_code')
if "HW" not in provider_code:
raise forms.ValidationError("Invalid provider code")

if "9" not in provider_etqe_id:
raise forms.ValidationError("Invalid ETQE ID")

return self.cleaned_data

class ProviderAdmin(admin.ModelAdmin):
form = ProviderForm

admin.site.register(Provider, ProviderAdmin)

(The first validation, the start date and end date, does work perfectly - the second validation does not work at all, and allows the form to be saved no matter what data is entered)

Then I also have replicated this for another form:

class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'

def clean(self):

person_alternate_id = self.cleaned_data.get('person_alternate_id')
alternate_id_type_id = self.cleaned_data.get('alternate_id_type_id')

if person_alternate_id is not None and alternate_id_type_id == 533:
raise forms.ValidationError("Person Alternate ID is not blank, therefore Alternate ID Type ID cannot be 533")
if person_alternate_id is None and alternate_id_type_id != 533:
raise forms.ValidationError("Person Alternate ID is blank, therefore Alternate ID Type ID must be 533")

return self.cleaned_data

class PersonAdmin(admin.ModelAdmin):
form = PersonForm

admin.site.register(Person, PersonAdmin)

This does not work at all, and also allows the form to be saved even if the invalid data has been entered.

Thanks in advance!

Julio Biason

unread,
Jan 4, 2018, 7:59:16 AM1/4/18
to django...@googlegroups.com
Hi Dean,

You forgot to call the form clean method (the first line after your `def clean` should be `super().clean()` as shown in the documentation: https://docs.djangoproject.com/en/2.0/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6d1e21bc-cc16-42bd-a9b9-949a800c81b7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Julio Biason, Sofware Engineer
AZION  |  Deliver. Accelerate. Protect.
Office: +55 51 3083 8101  |  Mobile: +55 51 99907 0554

dean raemaekers

unread,
Jan 5, 2018, 12:09:33 AM1/5/18
to Django users
Thanks Julio!

This worked.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages