I had a little trouble trying various information on the Web, so I made a note Checked with Django 1.10 and python 3.6.0 below
local.py
import sys
import os
import django
sys.path.append('/Path/to/your/project')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rootdir.settings') #My settings.py
def call():
django.setup()
from yourapp.models import YourModel
objects = YourModel.objects.all()
for obj in objects:
print(obj)
if __name__ == '__main__':
call()
django.setup()
To
from yourapp.models import YourModel
If you don't do it first
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Is said.
`os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rootdir.settings')`
↑ I felt like I was told something like ↓ in connection with this setting, but I forgot the details.
>```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.```
-
>```django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.```
***
Pycharm doesn't go to see the contents of sys.path.append
At the right end of the Preferences-> Project: yourproject-> Project Structure screen
Specify the project with Add Content Root.
Recommended Posts