How to Solve Error Message RuntimeError: Model class myproject.models.MyModel doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.

Posted on

Introduction

This article is an article where the main focus is to solve an error message exist upon running Django-based application. The error itself appear upon defining a model in a views.py file. Apparently, upon executing the application, the following error appear when executing the command ‘python manage.py runserver’ :

(env) C:\programming\python\myproject>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\programming\python\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\programming\python\env\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\programming\python\env\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\programming\python\env\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\programming\python\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\programming\python\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\programming\python\env\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\programming\python\env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\programming\python\env\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\programming\python\env\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\programming\python\env\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\programming\python\myproject\myproject\urls.py", line 19, in <module>
from . import views
File "C:\programming\python\myproject\myproject\views.py", line 5, in <module>
from .forms import MyForm
File "C:\programming\python\myproject\myproject\forms.py", line 4, in <module>
from .models import MyModel
File "C:\programming\python\myproject\myproject\models.py", line 8, in <module>
class Personnel(models.Model):
File "C:\programming\python\env\lib\site-packages\django\db\models\base.py", line 113, in __new__
raise RuntimeError(
RuntimeError: Model class myproject.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Solution

The error appear because of the model with the name ‘MyModel’ does not exist. The above project actually has more than one application. Let’s say there are two different applications. The first one ‘myapp’ and the second one is ‘sysapp’. The following is the structure describing it :

C:\programming\python\myproject>tree .
Folder PATH listing for volume Windows
Volume serial number is 00000069 E003:3593
C:\PROGRAMMING\PYTHON\MYPROJECT
├───.vscode
├───latihan
├───myapp
│ ├───migrations
│ │ └───__pycache__
│ ├───templates
│ │ └───myapp
│ └───__pycache__
├───myproject
│ ├───externals
│ │ ├───apps
│ │ └───libs
│ ├───requirements
│ ├───site_static
│ ├───templates
│ └───__pycache__
├───sysapp
│ ├───migrations
│ │ └───__pycache__
│ ├───templates
│ └───__pycache__
└───templates
├───flatpages
├───myapp
├───sysapp
└───temporary

C:\programming\python\myproject>

Instead of defining the model in the ‘sysapp’ app, the definition process is in the ‘myapp’ app. So, although both of the application already exist in the ‘settings.py’ as an installed apps, the ‘sysapp’ application cannot find the model in the application. This is the ‘forms.py’ in ‘sysapp’ app :

Rather than defining the ‘MyModel’ in the ‘models.py’ in ‘sysapp’ app, the definition is in the ‘models.py’ in ‘myapp’ app. So, the cause for the above error basically because of the non-existence of the model. There are several reason for the model non-existence :

  1. The model is not available in the first place. as it is supposed to be in the ‘sysapp’ file’s of ‘models.py’.

  2. There is a misspelled or mistyped of the model name. The name definition in ‘models.py’ file is not correct.

  3. The application where the model exist in not registered in the project file’s configuration which is normally exist in ‘settings.py’. The following is the snippet configuration part for defining it :

  4. INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        'django.contrib.flatpages',
        'crispy_forms',
        'sysapp',
    ]
  5. Next, the following is the actual content of ‘models.py’ check the content first :

    class MyModel(models.Model):
        name = models.CharField(max_length=100)
        def __str__(self):
            return self.name
    
  6. Now, going on to the next important file is the file with the name of ‘forms.py’. It is a file which is using the model. But rather than using it in the ‘forms.py’ exist in ‘sysapp’ app, a mistake is made by defining it in the ‘forms.py’ in the ‘myapp’ app.

So, in order to solve it, first of all just check the model location and also the spelling of the model’s name. Continue on, make sure that the app holding the model is already defined in the ‘settings.py’ file of the project. Last but not least, off course using the model in the right place. In other words, importing it from the right folder or app.

Leave a Reply