我跟隨此link在我的獨立應用程序在Python中使用Django的ORM。 manage.py sql'application_name'和manage.py syncdb運行良好,並且在執行syncdb時創建了表。我遇到的問題是當我運行實際的腳本(對於這種情況,它是dparser.py),它將處理數據庫事務,我遇到「TypeError:相對導入需要'包'參數」。下面是堆棧跟蹤:Django ORM獨立應用程序
Traceback (most recent call last):
File "dparser.py", line 23, in <module>
from dmodel.models import *
File "/home/<user>/d/dapp/dmodel/models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 93, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 28, in import_module
raise TypeError("relative imports require the 'package' argument")
TypeError: relative imports require the 'package' argument
下面是我的settings.py,models.py和dparser.py的內容:
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'd',
'USER': 'root',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
INSTALLED_APPS = ("dapp.dmodel",)
models.py:
from django.db import models
from django.db.models import Max
class Topics(models.Model):
topic_id = models.AutoField(primary_key=True)
topic = models.CharField(max_length=1000)
class Links(models.Model):
link_id = models.AutoField(primary_key=True)
topic = models.ForeignKey(Topics)
link = models.CharField(max_length=1000)
def getLastId(tag):
...
return lastid
dparser.py(部分):
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = ".settings.py"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'd',
'USER': 'root',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
INSTALLED_APPS = ("dapp.dmodel",)
from dmodel.models import * # --> This is were the exception occurs
希望有人能幫助我在這裏。提前致謝!
我不認爲你需要的行「從django.db.models導入最大」 – aglassman
我需要,因爲我在models.py – jaysonpryde
的getLastId函數上使用它,我會建議使用SQLAlchemy獨立的應用程序,而不是。這是Web框架不可知論者。 – Keith