How to Solve Error Message django.core.exceptions.FieldError: Unknown field(s) specified for Model

Posted on

Introduction

This is an article where the main focus is just to solve an error message. The error message is quite clear where it is exist as part of the title. The following is a file with the name of ‘forms.py’. It is a file which is becoming part of a Django-based application.

from django import forms
from .models import Assignment, Organization

class OrganizationForm(forms.ModelForm):
    class Meta:
        model = Organization
        fields = ('unit',)

The above file is what the cause of the error message appearing. It exist as part of the output appear in the execution of the Django-based internal service.

(winenv) C:\django\project>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 11, 2022 - 19:28:37
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.
...
...
...
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (unit) specified for Organization

Solution

Apparently, the solution is very easy. It is very clear from the error message exist in the introduction part. It is because there are no field with the name of ‘unit’. So, in order to solve the problem, the following are the order or the sequence of the action to do it :

  1. So, first of all, just find the property available for that model.

  2. In this context, the model name is Organization. Just look at the ‘models.py’ of the application. Check the model with the name ‘Organization’.

  3. Check for the available property in that model. For an example, in this case the command property will be ‘id’. So, change the form class to have the available property. The following is the modification of it : </p

    from django import forms
    from .models import Assignment, Organization
    
    class OrganizationForm(forms.ModelForm):
        class Meta:
            model = Organization
            fields = ('id',)
    
  4. Last but not least, just refresh the page and check the log output from the Django-based internal service execution. If there are no more errors appear, the solution will definitely work.

Leave a Reply