How to Solve Error Message AttributeError: ‘Model’ object has no attribute ‘urls’ when running Django Application

Posted on

Introduction

This is another article where the content is about how to solve a certain error message. That error message exist in the title of this article. The error message is ‘Model’ object has no attribute ‘urls’. The full complete error message appear as an output of the running Django internal service as follows :

C:\django\project\apps> python manage.py runserver 
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 01, 2022 - 10:04:19
Django version 3.2.6, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
...
...  base\admin.py changed, reloading 
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'Topic' object has no attribute 'urls'
C:\django\project\apps\base\admin.py changed, reloading

The error above appear when there is a new line addition in the ‘admin.py’ file. The following is that additional line :

admin.site.register(Room, Topic)

It is definitely the ‘admin.py’ new additiona line since in the output of the running internal Django server shows it.

Solution

So, since the above line actually triggering the error, the solution is easy. It means that registering the model for further access and management in the administration module cannot be working in that kind of declaration or registration. It must be each model for each declaration. Although importing the model can be in one line for all of the available models such as below :

from .models import Room, Topic, Message

But it is not working and it does not have the same logic when registering the model as follows :

admin.site.register(Room, Topic)

So, in order to solve the problem and error message, just do the correction in the above line exist as follows :

admin.site.register(Room)
admin.site.register(Topic)

Run the Django-based application again and access the administration module page. If there are no futher error, it means the correction works perfectly fine as the solution.

Leave a Reply