Introduction
This is an article where the main focus is to show how to solve an error message appear. Actually, it is an error message which appear upon the modification of a Django application’s source code. Upon the modification, there is an error message appear in the log output console :
... File "C:\project\project\apps\urls.py", line 2, in from apps import views File "C:\project\project\apps\views.py", line 4, in from .forms import EmployeeForm File "C:\project\project\apps\forms.py", line 4, in class EmployeeForm(ModelForm): File "C:\project\env\lib\site-packages\django\forms\models.py", line 295, in __new__ raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form EmployeeForm needs updating.
In the above output, it is actually an output which is part of the log appear. It is a log which appear after the Django application is running in the console. Upon editing a class with the name of ‘EmployeeForm’ for defining entry from for inserting Employee data, the above error message appear. Before going on further, below is the structure of the Django project and Django application :
C:\project>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\project 09/06/2022 07:13 AM <DIR> . 09/06/2022 10:42 AM <DIR> .. 09/06/2022 08:19 PM <DIR> apps 08/24/2022 09:30 AM 159,744 db.sqlite3 08/20/2022 05:17 PM 683 manage.py 08/20/2022 05:18 PM <DIR> project 09/06/2022 07:14 AM 126 requirements.txt 3 File(s) 160,553 bytes 4 Dir(s) 65,556,508,672 bytes free C:\project>
After describing the Django project structure, below is the descriptioni of the Django application which is being the one triggering the error message :
(env) C:\project\apps>dir Volume in drive C is Windows-SSD Volume Serial Number is CA30-19A4 Directory of C:\project\apps 09/06/2022 08:19 PM <DIR> . 09/06/2022 07:13 AM <DIR> .. 08/24/2022 10:02 AM 215 admin.py 08/24/2022 08:43 AM 146 apps.py 08/26/2022 02:19 PM <DIR> migrations 08/26/2022 02:05 PM 1,548 models.py 09/06/2022 07:01 PM <DIR> templates 08/24/2022 08:43 AM 63 tests.py 09/06/2022 08:03 AM 118 urls.py 09/08/2022 06:43 AM 871 views.py 08/24/2022 08:43 AM 0 __init__.py 09/08/2022 06:43 AM <DIR> __pycache__ 7 File(s) 2,961 bytes 6 Dir(s) 66,276,225,024 bytes free (env) C:\project\apps>
How to Solve Error Message django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the ‘fields’ attribute or the ‘exclude’ attribute is prohibited
In this article, the main error message which is hinting or pointing out to the solution exist in the last part of the output above. It is exist as in the following part :
raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form EmployeeForm needs updating.
As it is exist in the above output part of the log console from running Django application, there is a hint to solve the problem. There is a detection which is indicating that in the Django application, there is a ModelForm which neither it does not have any ‘fields’ attribute nor it has a prohibited ‘exclude’ attribute. In that case, the following is the actual step for solving the problem according to the hint as follows :
-
First of all, reading and checking the file with the name of ‘forms.py’ which exist in the application folder. In this context, it is the ‘apps’ folder.
from django.forms import ModelForm from .models import Employee class EmployeeForm(ModelForm): class Meta: model = Employee
Without having to look it further to the other file, the trigger of the error is already clearly appear. The reason is because in the EmployeeForm class it does not have any ‘fields’ attribute as part of the definition. So, add the ‘fields’ attribute in the EmployeeForm class definition. But in order to do that, make sure to check the Employee class model further which exist in ‘models.py’.
-
Next after that, continuing to edit the ‘forms.py’ in order to add the ‘fields’ attribute to the ‘EmployeeForm’ class, just check the ‘Employee’ class model in ‘models.py’ file. Actually the following is the actual content of the ‘models.py’ file of ‘Employee’ class :
class Employee(models.Model): name = models.CharField(max_length=255, default="")
Apparently, there is only one field attribute of the Employee class exist at the moment. So, just edit the ‘EmployeeForm’ by adding the ‘fields’ attribute which including all field attribute of the Employee model class as part of the EmployeeForm model form class. Just add the following line :
fields = '__all__'
Using the additional line above, below is the revision of the ‘EmployeeForm’ class :
from django.forms import ModelForm from .models import Employee class EmployeeForm(ModelForm): class Meta: model = Employee fields = '__all__'