I have a Django admin form which has an known source of errors that prevent the form from being saved. I have checked all the fields in the model, and all the required fields have been filled out. Nevertheless, I continue to get the standard error message:
Please correct the error below.
at the top of the form, yet no fields are highlighted with an error message.
What can be another cause, and how do I debug this?
Here is how you can see what the hidden errors are. Above your form class (create one if it doesn't already exist), add these imports:
# get a way to log the errors:
import logging
log = logging.getLogger(__name__)
# convert the errors to text
from django.utils.encoding import force_text
Then add to your form class this def:
class MyAdminForm(forms.ModelForm):
def is_valid(self):
log.info(force_text(self.errors))
return super(MyAdminForm, self).is_valid()
When you examine your log after submitting again, it will list all the hidden errors.
When I had this problem, I found the cause of the errors were some 'calculated' read-only fields that I had added to the form, e.g.:
get_num_items = forms.IntegerField()
The validation failed since these fields had no value as required. Changing the field definition to include required=False fixed the problem:
get_num_items = forms.IntegerField(required=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With