我可以在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
并使用设置确实有效。为什么不在这个脚本中做同样的事情?谢谢
答案 0 :(得分:2)
你可能错过了这个(我认为mysite不应该在设置行中):
import os, sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import django
django.setup()
for item in django.apps.all_models:
print(item)