In the daily development of Django, a separate PY file is often created to test the written method.
# mysite/main.py#-*-coding:utf-8-*-from deploy.getinfo import *from getruntime import *def Main (): # get domain Print GetDomain () # gets the domain name corresponding to the rule print getdomainrewrite ("1") if __name__ = = ' __main__ ': Main ()
Start Django times wrong
Traceback (most recent): File "E:\MyEclipse_work\ops\deploy\main.py", line <module> main () Fil E "E:\MyEclipse_work\ops\deploy\main.py", line A, in main print getdomain () File "E:\MyEclipse_work\ops\deploy\deploy \getinfo.py ", line ten, in GetDomain return Json.dumps (List (Deploy.objects.filter (). VALUES (" Domain_id__domain_name "," domain_id ")) File" C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\query.py ", line 640, in Values return Self._clone (Klass=valuesqueryset, Setup=true, _fields=fields) File "C:\Python27\lib\site-packages\ django-1.8-py2.7.egg\django\db\models\query.py ", line 992, in _clone c._setup_query () File" C:\Python27\lib\site-pack ages\django-1.8-py2.7.egg\django\db\models\query.py ", line 1169, in _setup_query self.query.add_fields (self.field_ Names, True) File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\sql\query.py", line 1660, in Add_fields Name.split (LOOKUP_SEP), opts, alias, ALLOW_MANY=ALLOW_M2M) File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\sql\query.py", line 1443, in Setup_joins names, opts, Allow_many, fail_on_missing=true) File "C:\Python27\lib\site-packages\django-1.8-py2 .7.egg\django\db\models\sql\query.py ", line 1347, in Names_to_path field, model, _, _ = Opts.get_field_by_name (name) F Ile "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py", line 406, in Get_field_by_ Name cache = Self.init_name_map () File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\ options.py ", line 435, in Init_name_map for F, model in Self.get_all_related_m2m_objects_with_model (): File" C:\Python 27\lib\site-packages\django-1.8-py2.7.egg\django\db\models\options.py ", line 520, in Get_all_related_m2m_objects_ With_model cache = Self._fill_related_many_to_many_cache () File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg \django\db\models\options.py ", line 534, in _fill_related_many_To_many_cache for Klass in Self.apps.get_models (): File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\ utils\lru_cache.py ", line 101, in wrapper result = User_function (*args, **kwds) File" C:\Python27\lib\site-packages\dj ango-1.8-py2.7.egg\django\apps\registry.py ", line 168, in Get_models Self.check_models_ready () File" C:\Python27\lib\ site-packages\django-1.8-py2.7.egg\django\apps\registry.py ", line 131, in Check_models_ready raise Appregistrynotready ("Models aren ' t loaded yet.") django.core.exceptions.AppRegistryNotReady:Models aren ' t loaded yet.
The final error is: django.core.exceptions.AppRegistryNotReady:Models aren ' t loaded yet.
The error message comes from Registry.py's assertion.
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py", line 131, in Check_models_ready Raise Appregistrynotready ("Models aren ' t loaded yet.")
So view the C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\apps\registry.py file in view raise Appregistrynotready
def check_models_ready (self): "" " raises a exception if all models haven ' t been imported yet. " " If not self.models_ready: raise Appregistrynotready ("models aren ' t loaded yet.")
Annotations show that the cause of the exception is because models is not being import
Then, at the beginning of the program, the import added
From deploy.models Import *
Again run the error is still.
Find descriptions of Standalone scripts When searching for Appregistrynotready exceptions in official documents.
To put it simply, theDjango Test relies on the DJANGO_SETTINGS_MODULE environment variable. If we want to test Django with a separate PY script, we should start initializing the Django environment in our program. Otherwise it will be reported Appregistrynotready exception.
# Initialize command import djangodjango.setup ()
After the increase, it runs normally.
The Django.setup () source code is immediately viewed.
From django.utils.version Import get_versionversion = (1, 8, 0, ' alpha ', 0) __version__ = get_version (version) def setup (): c0/> "" " Configure the settings (this happens as a side effect of accessing the first setting), Configure logging A ND populate the App registry. "" From Django.apps import apps from django.conf import settings from django.utils.log import configure_logging configure_logging (settings. Logging_config, settings. LOGGING) apps.populate (settings. Installed_apps)
Official description of the Django.setup () method
Reference documents
https://docs.djangoproject.com/en/1.8/releases/1.7/#app-loading-changes
https://docs.djangoproject.com/en/1.8/ref/applications/#django. Setup
Django 1.8 django.core.exceptions.AppRegistryNotReady:Models aren ' t loaded yet.