我有一個工作正常的Django應用程序。我希望能夠利用該模型從另一個(獨立的)python應用程序訪問數據庫。這裏是我有什麼(不工作)。在Django框架之外使用django模型
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框架內工作。我錯過了什麼?你的模型
import django
import sys
import os
sys.path.append(os.path.abspath("/home/pi/garageMonitor/django/garageMonitor"))
os.environ['DJANGO_SETTINGS_MODULE'] = 'garageMonitor.settings'
django.setup()
import models
config = models.SystemConfiguration.objects.filter(idSystemConfiguration=1)
config = config[0]
for x in config.__dict__:
print x
https://docs.djangoproject.com/en /1.9/ref/models/options/#app-label也許在你的模型'meta'中添加'app_label'? –
爲什麼你想從外面訪問模型?爲了測試目的? – DhiaTN
您正在使用哪個Django版本? – knbk