【发布时间】:2016-03-21 21:50:24
【问题描述】:
我有一个运行良好的 django 应用程序。我希望能够利用该模型从另一个(独立)python 应用程序访问数据库。这是我所拥有的(不起作用。)
import sys
import os
sys.path.append(os.path.abspath("/home/pi/garageMonitor/django/garageMonitor"))
os.environ['DJANGO_SETTINGS_MODULE'] = 'garageMonitor.settings'
import models
config = models.SystemConfiguration.objects.filter(idSystemConfiguration=1)
config = config[0]
for x in config.__dict__:
print x
这是我得到的错误:
File "/home/pi/garageMonitor/django/lib/webWatcher.py", line 14, in <module>
import models
File "/home/pi/garageMonitor/django/garageMonitor/models.py", line 11, in <module>
class DoorClosing(models.Model):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 131, in __new__
'app.' % (new_class.__name__, model_module.__name__)
django.core.exceptions.ImproperlyConfigured: Unable to detect the app label for model "DoorClosing
DoorClosing 是我的 models.py 文件中的一个类。类似的代码在 django 框架中工作。我错过了什么?
【问题讨论】:
-
docs.djangoproject.com/en/1.9/ref/models/options/#app-label 也许在您的模型中
meta添加app_label? -
为什么要从外部访问模型?用于测试目的??
-
你使用的是哪个 Django 版本?