I have installed the cities_light library in Django and populated the db with the cities as instructed in the docs. I added the app in INSTALLED_APPS and I have been able to pull the data in this simple view. All cities load as expected:
def index(request):
cities = City.objects.all()
context = {
'cities': cities
}
return render(request,'templates/index.html',context)
However, I am trying to create a model which has City as a foreign key, but when I run the app or try to make the migrations I get 'django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.'.
from cities_light.admin import City
from django.db import models
class Home(models.Model):
location = models.ForeignKey(City, on_delete=models.CASCADE)
I suspect I might need to override the model. Would that be the case?
CodePudding user response:
In settings.py file add below code at the end of file:
SOUTH_MIGRATION_MODULES = {
'cities_light': 'cities_light.south_migrations',
}
I think you did not add above code in settings.py file that's why you got that error.
Data update
Finally, populate your database with command:
./manage.py cities_light
CodePudding user response:
The same error appears after adding the south migration modules. I already had the database populated and I used it successfully for my view. Here is the settings.py content:
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-n=_c6^ui9$e&t jsy(&eewc%n1rux91z3f94jwrwrq91w7^c43'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'cities_light',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'CBV_practice.web',
'CBV_practice.auth_app',
]
CITIES_LIGHT_INCLUDE_COUNTRIES = ['BG']
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'CBV_practice.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates']
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'CBV_practice.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cbv_practice_db_try_2',
'USER': 'postgres-user',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SOUTH_MIGRATION_MODULES = {
'cities_light': 'cities_light.south_migrations',
}
The whole error:
Traceback (most recent call last):
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\manage.py", line 22, in <module>
main()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\core\management\__init__.py", line 420, in execute
django.setup()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\apps\registry.py", line 116, in populate
app_config.import_models()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\apps\config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\marti\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\CBV_practice\web\models.py", line 1, in <module>
from cities_light.admin import City
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\cities_light\admin.py", line 7, in <module>
from . import forms
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\cities_light\forms.py", line 5, in <module>
Country, Region, SubRegion, City = get_cities_models()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\cities_light\loading.py", line 17, in get_cities_models
return [get_cities_model(model_name) for model_name in model_names]
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\cities_light\loading.py", line 17, in <listcomp>
return [get_cities_model(model_name) for model_name in model_names]
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\cities_light\loading.py", line 12, in get_cities_model
return get_model(CITIES_LIGHT_APP_NAME, model_name, *args, **kwargs)
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\apps\registry.py", line 201, in get_model
self.check_models_ready()
File "C:\Users\marti\PycharmProjects\WebDevelopmentCourseDjango\WEB FRAMEWORK\CBV_practice\venv\lib\site-packages\django\apps\registry.py", line 143, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.