How to Solve Error Message AttributeError at /url/ in Django Application

Posted on

Introduction

On accessing a specific URL address of a Django-based application, there is an error message appear. The appearance of the error message exist in the page as follows :

AttributeError at /org/
type object 'Employee' has no attribute 'object'
Request Method:GET
Request URL:http://localhost:8000/org/
Django Version:3.2.6
Exception Type:AttributeError
Exception Value:type object 'Employee' has no attribute 'object'
Exception Location:C:\programming\python\django\myproject\org\views.py, line 9, in index
Python Executable:C:\app\python39\python.exe
Python Version:3.9.7
Python Path:['C:\\programming\\python\\django\\myproject',
 'C:\\app\\python39\\python39.zip',
 'C:\\app\\python39\\DLLs',
 'C:\\app\\python39\\lib',
 'C:\\app\\python39',
 'C:\\app\\python39\\lib\\site-packages']
Server time:Sat, 23 Oct 2021 14:55:17 +0000
Define a function that is not exist. Using object instead of objects.
Internal Server Error: /org/
Traceback (most recent call last):
File "C:\app\python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\app\python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\programming\python\django\myproject\org\views.py", line 9, in index
context = Employee.object.all()
AttributeError: type object 'Employee' has no attribute 'object'
[23/Oct/2021 21:55:17] "GET /org/ HTTP/1.1" 500 65447
Not Found: /favicon.ico
[23/Oct/2021 21:55:17,732] - Broken pipe from ('127.0.0.1', 62757)

Solution

Basically, the solution for the problem is very simple. Since the error message is very clear. It is very easy to solve it. It is just a simple mistake by miss-typed syntax. The mistake itself is pointing at the following line :

context = Employee.object.all()
AttributeError: type object 'Employee' has no attribute 'object'

Actually, the error message informing that the model class with the name of ‘Employee’ does not have any atribute with the name of ‘object’. It should be objects instead of object. So, the solution is available just by changing the keyword ‘object’ with ‘objects’. It is a default attribute exist as part of the default model which is very important to retrieve objects from an instance of the models. In the above example, it is using to retrieve all instances of the model with the name of ‘Employee’ with the additional filter function of all(). Change the line as follows :

context = Employee.objects.all()

Soon after, just execute the Django-based application once more and access the page.

One thought on “How to Solve Error Message AttributeError at /url/ in Django Application

Leave a Reply