简体   繁体   中英

Import error in the django tutorial

I'm trying to follow the django tutorial and create two tables where the unique key of table1 might appear several times on table2 (which has a different unique key)

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,
)

when trying to run:

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")

I get the error stack (I post only the end, if you need more, tell me please):

  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

I assume it's a problem with the imports, but how do I solve it?

If package is the project name and MyApp the app for that project you don't have to import package in your files, you can just:

from MyApp.models import apples, oranges

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

This is an error too:

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

apples and oranges are inside your models.py

Perhaps you also need to read about modules and packages .

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM