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!

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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.

Reply via email to