Errors in fields not displaying on Django admin form?

0 votes

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?

Apr 29 in Python by kartik
• 11,890 points

1 answer to this question.

0 votes


Here is how you can see what the hidden errors are. Above your form class, 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):
        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)

Hope this works!!

Thank You!!

answered Apr 29 by Niroj
• 23,950 points

Related Questions In Python

0 votes
1 answer

How to run django unit-tests on production database in Python?

In case someone googles here searching for ...READ MORE

answered Nov 28, 2018 in Python by Nymeria
• 3,540 points
0 votes
1 answer

Raw_input method is not working in python3. How to use it?

raw_input is not supported anymore in python3. ...READ MORE

answered May 4, 2018 in Python by aayushi
• 750 points
0 votes
1 answer

Django form validation: making “required” conditional?

This is done with the clean method on the ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 57,530 points
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
0 votes
1 answer

Model Form “object has no attribute 'cleaned_data'” in Django?

Hello @kartik, For some reason, you're re-instantiating the ...READ MORE

answered Apr 29 in Python by Niroj
• 23,950 points
0 votes
1 answer

How to load a custom JS file in Django admin home?

Hello @kartik, You can override templates/admin/index.html and add the JavaScript ...READ MORE

answered May 14 in Python by Niroj
• 23,950 points