When I try to access django's model directly from the interpreter, I get angry with the following error:
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
To prevent this, it seems that you need to call the following before importing the model. Calling it later will result in an error.
django.setup()
In addition, you need to set environment variables
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_name.settings')
'app_name.settings'
The summary is as follows.
>>> import django
>>> import os
>>> os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_name.settings')
'app_name.settings'
>>> django.setup()
>>> from APP_NAME.models import YourModel
>>> YourModel.objects.all()
<QuerySet []>
Recommended Posts