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, 2020 in Python by kartik
• 37,520 points
4,688 views

1 answer to this question.

0 votes

Hiii,

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):
        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)

Hope this works!!

Thank You!!

answered Apr 29, 2020 by Niroj
• 82,840 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,560 points
1,573 views
0 votes
1 answer

Error:django-debug-toolbar breaking on admin while getting sql stats

Hello @kartik, You just need to be able ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,840 points
1,301 views
+1 vote
1 answer

Error:Getting Site Matching Query Does Not Exist Error after creating django admin

Hello @kartik, The Site object for your Django project is ...READ MORE

answered Jun 22, 2020 in Python by Niroj
• 82,840 points
12,596 views
0 votes
1 answer

How to do a not equal in Django queryset filtering?

Hello @kartik, Your query appears to have a ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,840 points
28,507 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,468 views
0 votes
1 answer
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, 2020 in Python by Niroj
• 82,840 points
15,972 views
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, 2020 in Python by Niroj
• 82,840 points
8,015 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP