我的Django应用文件夹中有models.py
和test.py
文件。
我的项目名为strava
,应用程序为explorer_api
如何将模型导入“ test.py”?
我尝试过:
from strava.explorer_api.models import Activity
from explorer_api.models import Activity
from .models import Activity
from models import Activity
但是:
SystemError:父模块“”未加载,无法执行相对 导入
结构:
/
admin.py
apps.py
collector.py
models.py
serializers.py
test.py
tests.py
urls.py
views.py
__init__.py
migrations/
0001_initial.py
__init__.py
__pycache__/
0001_initial.cpython-35.pyc
__init__.cpython-35.pyc
templates/
explorer_api/
index.html
save.html
__pycache__/
admin.cpython-35.pyc
collector.cpython-35.pyc
models.cpython-35.pyc
serializers.cpython-35.pyc
urls.cpython-35.pyc
views.cpython-35.pyc
__init__.cpython-35.pyc
答案 0 :(得分:1)
看看这个Django script to access model objects without using manage.py shell 如果strava是您的项目目录,则
大于或等于Django 1.9-1.10
import sys, os, django
sys.path.append('full_path_to_strava_directory')
os.environ['DJANGO_SETTINGS_MODULE'] = 'strava.settings'
django.setup()
from strava.activity.models import Activity
Django 1.11之前的版本
from strava.wsgi import application
from strava.activity models import Activity
答案 1 :(得分:0)
您的INSTALLED_APPS中是否有explorer_api
?
此外,在tests.py
中仅保留一次导入:
from explorer_api.models import Activity