从 shell 获取 django.apps 但不是脚本
getting django.apps from shell but not script
我可以在 shell:
中获取我的应用程序信息
In [1]: from django.apps import apps
In [2]: info = apps.all_models
In [3]: for item in info:
print(item)
...:
mailer
auth
analytics
subscribe
staticfiles
contenttypes
layout
contact
但不在脚本中。脚本在主站点文件夹中,即 manage.py
,而设置文件在 mysite/project/settings.py
中。脚本就在mysite
,项目文件夹上面:
#!/usr/bin/env python
# coding: utf-8
import os, sys, subprocess, time, re, ast
# This should be the directory that contains the directory containing settings.py
# sys.path.add('/path/to/mysite')
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
HOMEPATH = os.path.expanduser("~")
PROJECT_PATH = "{}/work_projects/mysite".format(HOMEPATH)
ENTRY_PATH = os.path.join(PROJECT_PATH, "project")
MOMMY_PATH = os.path.join(PROJECT_PATH, "model_mommy")
MOMMY_TESTPATH = os.path.join(MOMMY_PATH, "tests")
sys.path.append(ENTRY_PATH)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.project.settings")
from django.apps import apps
info = apps.all_models
for item in info:
print(item)
没有输出,信息为空白。导入 django.conf.settings
并使用这些设置确实有效。为什么在这个脚本中做同样的事情不起作用?谢谢
您可能错过了这个(我认为我的网站不应该在设置行中):
import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import django
django.setup()
for item in django.apps.all_models:
print(item)
我可以在 shell:
中获取我的应用程序信息In [1]: from django.apps import apps
In [2]: info = apps.all_models
In [3]: for item in info:
print(item)
...:
mailer
auth
analytics
subscribe
staticfiles
contenttypes
layout
contact
但不在脚本中。脚本在主站点文件夹中,即 manage.py
,而设置文件在 mysite/project/settings.py
中。脚本就在mysite
,项目文件夹上面:
#!/usr/bin/env python
# coding: utf-8
import os, sys, subprocess, time, re, ast
# This should be the directory that contains the directory containing settings.py
# sys.path.add('/path/to/mysite')
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
HOMEPATH = os.path.expanduser("~")
PROJECT_PATH = "{}/work_projects/mysite".format(HOMEPATH)
ENTRY_PATH = os.path.join(PROJECT_PATH, "project")
MOMMY_PATH = os.path.join(PROJECT_PATH, "model_mommy")
MOMMY_TESTPATH = os.path.join(MOMMY_PATH, "tests")
sys.path.append(ENTRY_PATH)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.project.settings")
from django.apps import apps
info = apps.all_models
for item in info:
print(item)
没有输出,信息为空白。导入 django.conf.settings
并使用这些设置确实有效。为什么在这个脚本中做同样的事情不起作用?谢谢
您可能错过了这个(我认为我的网站不应该在设置行中):
import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import django
django.setup()
for item in django.apps.all_models:
print(item)