Introduction
This article is showing on how to solve a certain error exist. The error appear upon executing the Django application. The following is the condition of the Django application where the error appear :
-
First of all, there is a Django project exist. For checking out how to create a Django project just check How to Create Django Project in Microsoft Windows using Command Line.
-
Inside the Django project, it also exist a Django application. For a further reference, just look at the article for creating Django application in How to Create a Django Application inside a Django Project in Microsoft Windows.
-
In the Django application, just create a model. For an example, look at How to Create Model in Django Application.
-
After that, create a form by creating a file with the name of ‘forms.py’ first inside the Django application folder. Below is the structure of the Django project where it has a Django application inside of it :
C:\django\myproject>tree Folder PATH listing for volume Windows-SSD Volume serial number is CA30-19A4 C:. ├───myapp │ ├───migrations │ │ └───__pycache__ │ ├───templates │ │ └───myapp │ └───__pycache__ └───myproject └───__pycache__
Create the file ‘forms.py’ inside the myapp folder as exist in the above Django project directory structure.
-
Fill that ‘forms.py’ file with the following content :
from django.forms import ModelForm from django import forms from .models import Employee class EmployeeForm(ModelForm): name = forms.CharField(widget = forms.Textarea) class Meta: model = Employee fields = ['name','address','email'] def __init__(self, *args, **kwargs): super(EmployeeForm, self).__init__(*args, **kwargs) self.fields['name'].label = 'Name'
-
Do not forget to create an HTML file as a file acts as the view or the front end component.< Also define the URL for accessing the page in ‘urls.py’./p>
-
Next, there is a function definition in ‘views.py’ which is also exist in the Django application folder with the following content :
def employee(request): form = EmployeeForm context = {'string':'Hello Employee'} context = {'form':form} return render(request, "myapp/employee.html",context)
-
That function execute or render a template file which is exist in the templates folder of the Django application.
But unfortunately, despite of all the preparation, executing the Django application turns out triggering an error exist as follows :
C:\django\myproject\myapp\models.py changed, reloading. Watching for file changes with StatReloader Performing system checks... Exception in thread django-main-thread: Traceback (most recent call last): File "C:\python\python310\lib\threading.py", line 1016, in _bootstrap_inner self.run() File "C:\python\python310\lib\threading.py", line 953, in run self._target(*self._args, **self._kwargs) File "C:\django\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "C:\django\env\lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run self.check(display_num_errors=True) File "C:\django\env\lib\site-packages\django\core\management\base.py", line 475, in check all_issues = checks.run_checks( File "C:\django\env\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks new_errors = check(app_configs=app_configs, databases=databases) File "C:\django\env\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config return check_resolver(resolver) File "C:\django\env\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver return check_method() File "C:\django\env\lib\site-packages\django\urls\resolvers.py", line 494, in check for pattern in self.url_patterns: File "C:\django\env\lib\site-packages\django\utils\functional.py", line 57, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\django\env\lib\site-packages\django\urls\resolvers.py", line 715, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\django\env\lib\site-packages\django\utils\functional.py", line 57, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\django\env\lib\site-packages\django\urls\resolvers.py", line 708, in urlconf_module return import_module(self.urlconf_name) File "C:\python\python310\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1050, in _gcd_import File "<frozen importlib._bootstrap>", line 1027, in _find_and_load File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 688, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 883, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "C:\django\myproject\myproject\urls.py", line 18, in <module> from myapp import urls File "C:\django\myproject\myapp\urls.py", line 3, in <module> from . import views File "C:\django\myproject\myapp\views.py", line 2, in <module> from .forms import EmployeeForm File "C:\django\myproject\myapp\forms.py", line 5, in <module> class EmployeeForm(ModelForm): File "C:\django\env\lib\site-packages\django\forms\models.py", line 306, in __new__ fields = fields_for_model( File "C:\django\env\lib\site-packages\django\forms\models.py", line 181, in fields_for_model opts = model._meta AttributeError: type object 'Employee' has no attribute '_meta'. Did you mean: 'Meta'?
How to Solve Error Message AttributeError: type object ‘Object’ has no attribute ‘_meta’.
In order to solve the problem, after retracing back to the steps in the introduction part, finally find the trigger. Actually, the trigger is in the declaration of the the Django model. For further information, just look at How to Create Model in Django Application. But apparently, the model definition is the cause of the above error message. Below is the content of the ‘models.py’ file with the Django model declaration :
class Employee(): name = models.CharField(max_length=50) email = models.CharField(max_length=100) address = models.CharField(max_length=250) class Meta: db_table = 'employee'
After looking at the Django model syntax closely, there is something wrong with the declaration of the Django model. It lack an important parameter. It is actually need a ‘models.Model’ parameter to be able to declare the model is a Django model class. So, just revise the Django model definition as follows :
class Employee(models.Model): name = models.CharField(max_length=50) email = models.CharField(max_length=100) address = models.CharField(max_length=250) class Meta: db_table = 'employee'
If there are no more error appear, the Django application will be able to run properly.