在django教程中导入错误

时间:2011-11-08 14:43:29

标签: python django import

我正在尝试按照django教程创建两个表,其中table1的唯一键可能会在table2上出现多次(具有不同的唯一键)

CREATE TABLE "apples" (
    "id1" integer NOT NULL PRIMARY KEY,
    "value1" varchar(400) NOT NULL,
)
;
CREATE TABLE "oranges" (
    "id2" integer NOT NULL PRIMARY KEY,
    "id1" integer NOT NULL REFERENCES "MyApp_apples" ("id"),
    "value2" datetime NOT NULL,
)

尝试运行时:

import package
from package import MyApp
from package.MyApp import models
from package.MyApp import apples, oranges
p = apples.objects.get(id=1)
p.oranges_set.create(value2="2168164000000")

我得到了错误堆栈(我只发布了结尾,如果你需要更多,请告诉我):

  File "/usr/lib/pymodules/python2.7/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/usr/lib/pymodules/python2.7/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named MyApp

我认为这是导入的问题,但我该如何解决呢?

1 个答案:

答案 0 :(得分:3)

如果package是项目名称而MyApp该项目的应用程序,您不必在文件中导入包,您可以:

from MyApp.models import apples, oranges

p = apples.objects.get(id=1)
...

这也是一个错误:

from MyApp import models
from MyApp import apples, oranges // ImportError

applesoranges位于models.py

也许您还需要阅读modules and packages

希望有所帮助!