如何从应用程序外部使用Django模型?

2024-04-19 07:04:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Django模型DriverTrip。我的视图中没有任何内容,也没有URL。我使用Django作为数据库,使用脚本在数据库中存储内容。你知道吗

这是我的每一件事物的样子:

loadmngr/
    models.py
    views.py
    urls.py
    management/
         commands/
             > it.py <

假设在managementcommands中有init.py。你知道吗

我所做的只是简单地导入我的Django模型。这里是it.py

import sys
parent = '/Users/work/TM/loadmngr'
sys.path.insert(0, parent)
from models import Trip

我运行python manage.py it,得到一个RunTimeError

RunTimeError: Model class models.Driver doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded.

错误的后半部分...or else was imported before its application was loaded是我认为可能的问题所在。你知道吗

问题是:如何在正确配置设置的情况下在外部正确使用Django模型?你知道吗


Tags: djangopy模型import数据库内容applicationmodels