django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. The ultimate solution to the error

Recently, I needed to start writing a project, so I opened the CMDB project code that had been sealed in the computer for a year, and planned to do a secondary processing. Then transplant it to Linux and configure the environment as soon as it is executed... sour! Errors are reported everywhere! Gritting his teeth and stomping his feet, he changed all night. After a tough battle, most of the problems were resolved, but there was one mistake that could not be corrected no matter what. After rummaging through Google and Du Niang, there are even many answers that lead me to new pits. It's the following bug:

[root@CentOS CMDB]# python3 manage.py runserver
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f8a621f7730>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 94, in create
    module = import_module(entry)
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/visitor/桌面/CMDB/users/__init__.py", line 1, in <module>
    from users import models
  File "/home/visitor/桌面/CMDB/users/models.py", line 5, in <module>
    class superUser(models.Model):
  File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 110, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 247, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 125, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

It is this BUG, ​​the Internet search is all about adding django.setup(), the content of the error reported by that method is not exactly the same as mine, and it once took me into the pit. Apps aren't loaded yet The error report is a relatively large range. The method in this article is not applicable to you. Be sure to see if the error report at the beginning is consistent with mine.

Let's talk about the reasons for the error and the solution: Obviously, the error is that there is a problem with the class superUser (models.Model) of the models.py file. Obviously there is no problem here... So I commented all the models in the models. class, and then python manage.py check found that no errors were reported. At first, I felt that the problem was in it, but I couldn't see the problem here. Later, I thought that it was because of the models referenced in the __init__.py file from time to time. So I went to __init__.py to uncomment and found that it was feasible. F******K! I just found out that I wanted to execute a function during initialization, so I wrote it in the __init__.py file, but I was too lazy, and the function I wrote depended on the models.py file, so I had such a reference. So, just delete from users import models.

Summary: Do not add references to models in the __init__.py file of the App in django! ! ! Similarly, do not reference views and urls of the same level! ! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325111873&siteId=291194637