How to Solve Error Message ValueError at URL ModelForm has no model class specified when running Django Application

Posted on

Introduction

This is an article containing error message. The error message appear in the title of this article. That error message is ‘ ValueError at URL ModelForm has no model class specified’. The following is the actual error message appear as an output when running Django-based application internal server :

(winenv) C:\django\project>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
TestSystem check identified no issues (0 silenced).
February 05, 2022 - 08:09:16
Django version 3.2.6, using settings 'core.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[05/Feb/2022 08:10:38] "GET /apps/simple HTTP/1.1" 301 0
Internal Server Error: /apps/simple/
Traceback (most recent call last):
File "C:\django\project\winenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\django\project\winenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\django\project\app\views.py", line 150, in simple
form = OrganizationForm()
File "C:\django\project\winenv\lib\site-packages\django\forms\models.py", line 295, in __init__
raise ValueError('ModelForm has no model class specified.')
ValueError: ModelForm has no model class specified.
ERROR:django.request:Internal Server Error: /apps/simple/
Traceback (most recent call last):
File "C:\django\project\winenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\django\project\winenv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\django\project\apps\views.py", line 150, in simple
form = OrganizationForm()
File "C:\django\project\winenv\lib\site-packages\django\forms\models.py", line 295, in __init__
raise ValueError('ModelForm has no model class specified.')
ValueError: ModelForm has no model class specified.
[05/Feb/2022 08:10:38] "GET /apps/simple/ HTTP/1.1" 500 71685
ValueError at /apps/simple/
ModelForm has no model class specified.

Before going on further to the solution, the following is the source code which is pointing out where the error exist. The first one is the file with the name of ‘views.py’ :

def simple(request):
    if request.method == 'POST':
        form = OrganizationForm(request.POST)
        if form.is_valid():
            pass
    else:
        form = OrganizationForm()
    return render(request, 'apps/simple.html', {'form':form})

Following after, searching the other script file which is ‘forms.py’ where the content exist as follows :

class OrganizationForm(forms.ModelForm):
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('sysapp')
    logger.info(Organization.objects.all())
    organization_list = Organization.objects.all()
    context = {'organization_list':organization_list}
    program = forms.ModelChoiceField(label = 'Program', queryset = Program.objects.all(), initial = 0)
    class Meta:
        fields = ('unit',)

Solution

For the proper solution, the error message above pointing out by giving an information. The error message itself already hinting out about how to solve the error message. The error message is informing that the ‘ModelForm’ has no model class specified. In this context, the error line is pointing to the line which is defining the form as follows :

        form = OrganizationForm()

So, in order to solve it, just add the model which is associated to the form. The following is the line for solving the problem :

model = Organization

In conclusion, the revision of the forms.py for defining the form with the associated model will be as follows :

class OrganizationForm(forms.ModelForm):
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger('sysapp')
    logger.info(Organization.objects.all())
    organization_list = Organization.objects.all()
    context = {'organization_list':organization_list}
    program = forms.ModelChoiceField(label = 'Program', queryset = Program.objects.all(), initial = 0)
    class Meta:
        model = Organization
        fields = ('unit',)

Leave a Reply