Hi, I’m using djongo to connect my django project with my mongoDB. Everything works fine until I tried to define in the models.py some embedded Documents. When I did that I got the error “Models aren’t loaded yet”.
I would first like to know why is this error appearing and if someone can help me with this it would be great.
Python Code:
- Project Tree
(django-djongo) ➜ reports tree
.
├── db.sqlite3
├── manage.py
├── rep
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── __pycache__
│ │ ├── __init__.cpython-36.pyc
│ │ └── models.cpython-36.pyc
│ ├── tests.py
│ └── views.py
└── reports
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-36.pyc
│ ├── settings.cpython-36.pyc
│ ├── urls.cpython-36.pyc
│ └── wsgi.cpython-36.pyc
├── settings.py
├── urls.py
└── wsgi.py
5 directories, 19 files
(django-djongo) ➜ reports
- rep/models.py
0 from djongo import models
1 from django import forms
2
3
4
5 class Blog(models.Model):
6 name = models.CharField(max_length=100)
7 tagline = models.TextField()
8
9 class Meta:
10 abstract = True
11
12 class BlogForm(forms.ModelForm):
13 class Meta:
14 model = Blog
15 fields = ('name', 'tagline')
16
17 class Author(models.Model):
18 name = models.CharField(max_length=200)
19 email = models.EmailField()
20
21 class Meta:
22 abstract = True
23
24 class AuthorForm(forms.ModelForm):
25 class Meta:
26 model = Author
27 fields = ('name', 'email')
28
29
30 class Entry(models.Model):
31 blog = models.EmbeddedField(
32 model_container=Blog,
33 model_form_class=BlogForm
34 )
35 name = models.CharField(max_length = 225)
36 headline = models.CharField(max_length=255)
37 authors = models.ArrayField(
38 model_container=Author,
39 model_form_class=AuthorForm
40 )
- reports/settings.py
"""
Django settings for reports project.
Generated by 'django-admin startproject' using Django 2.2.12.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(!x9vch4j)txz0$22=s&7@%(u=w9k#5p6p#3-7wb)9k3k_l*^v'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rep'
]
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 = 'reports.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'reports.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'prueba',
'HOST': 'mongodb://127.0.0.1:27017'
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
- Traceback
(django-djongo) ➜ reports python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "/usr/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/jagl/Documents/Learning/Programming/Django/reports/rep/models.py", line 31, in <module>
class Entry(models.Model):
File "/home/jagl/Documents/Learning/Programming/Django/reports/rep/models.py", line 34, in Entry
model_form_class=BlogForm
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/djongo/models/fields.py", line 225, in __init__
super().__init__(model_container, *args, **kwargs)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/djongo/models/fields.py", line 87, in __init__
self._validate_container()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/djongo/models/fields.py", line 91, in _validate_container
for field in self.model_container._meta.get_fields():
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/db/models/options.py", line 734, in get_fields
return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/db/models/options.py", line 794, in _get_fields
all_fields = self._relation_tree
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/db/models/options.py", line 707, in _relation_tree
return self._populate_directed_relation_graph()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/db/models/options.py", line 678, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/apps/registry.py", line 178, in get_models
self.check_models_ready()
File "/home/jagl/.virtualenvs/django-djongo/lib/python3.6/site-packages/django/apps/registry.py", line 140, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.