我已经尝试了我看到的所有解决方案,但我仍然出现相同的错误。我正在尝试用以下文件中生成的一些假数据填充我的数据库:
Popular_MadLibs_app.py
import random
from madlibs_app.models import User
from faker import Faker
import django
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'madlibs_project.settings')
django.setup()
fakegen = Faker()
fname = ['Charles', 'Stephen', 'Mike', 'Cornell', 'John', 'Yemi', 'Tomiwa']
lname = ['Nurudeen', 'Ayotunde', 'Ajiteru',
'Kolade', 'Babatunde', 'Ifeanyi', 'Ola']
email_client = ['yahoo.com', 'gmail.com', 'outlook.com']
def add_user():
fname = random.choice(fname)
lname = random.choice(lname)
emailed = '{}.{}@{}'.format(fname, lname, random.choice(email_client))
ur = User.objects.get_or_create(
first_name=fname, last_name=lname, email=emailed)[0]
ur.save()
return ur
def populate(n=1):
for entry in range(n):
create_user = add_user()
if __name__ == '__main__':
print('Processing...')
populate(10)
print('Succesfully created!')
但我一直收到以下错误:
Traceback (most recent call last):
File "C:\Users\Madlibs\Desktop\madlibs\madlibs_project\population_madlibs_app.py", line 2, in <module>
from madlibs_app.models import User
File "C:\Users\Madlibs\Desktop\madlibs\madlibs_project\madlibs_app\models.py", line 6, in <module>
class User(models.Model):
File "C:\Users\Madlibs\anaconda3\envs\madlibs\lib\site-packages\django\db\models\base.py", line 127, in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\Madlibs\anaconda3\envs\madlibs\lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\Madlibs\anaconda3\envs\madlibs\lib\site-packages\django\apps\registry.py", line 138, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我查看了几种解决方案,包括cmd虚拟环境中的Set DJANGO_SETTINGS_MODULE=madlibs_project.settings,但仍然没有解决方案。
这是我的settings.py文件。
"""
Django settings for madlibs_project project.
Generated by 'django-admin startproject' using Django 4.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = Path.joinpath(BASE_DIR, "templates")
STATIC_DIR = Path.joinpath(BASE_DIR, "static")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-t-pby@4p3a%mpz2r_w3)d(7msdnrx@wl-yolws*hu5&owb7jq%"
# 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",
"madlibs_app.apps.MadlibsAppConfig",
]
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 = "madlibs_project.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [TEMPLATE_DIR],
"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 = "madlibs_project.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Password validation
# https://docs.djangoproject.com/en/4.1/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/4.1/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "static/"
STATICFILES_DIRS = [
STATIC_DIR,
]
# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
1条答案
按热度按时间qc6wkl3g1#
问题是因为您试图在
django.setup()
之前导入模型。尝试将您的代码更改为此代码,注意在导入模型之前移动了django.set()
。