Djangoのモデルデータをテスト用コードから呼び出した時のエラー
動作確認のために、普通のPythonスクリプトからDjango Modelを使おうとしたらエラーになった。
エラー1:
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
PyCharmの設定で、Run Configurations > Environment variables に
name:DJANGO_SETTINGS_MODULE
value:AppName.settings
value:settings
を足して解決。
エラー2:
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
モデルのインポートの前にdjango.setup()で解決。
from apps.app1.models import Model1
django.setup()