连接不同应用程序的模型 - Django

时间:2016-06-13 04:43:25

标签: python django web-applications django-models orm

鉴于我有4个模型:

  • Project
  • User(来自django.contrib.auth.models)
  • Sentence
  • Translation

我的django项目目录的结构如下:

 \myprojname
     \project
         models.py
     \data
         models.py

User模型独立于ProjectsSentences模型,但它通过ForeignKey链接到Projects模型:

myprojname/projects/models.py看起来像这样:

from django.db import models
from django.contrib.auth.models import User

class Project(models.Model):
    project_id = models.AutoField(primary_key=True)
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)

    name = models.CharField(max_length=140)
    # Admin user for the particular *Project* object.
    # Have the rights to write to the *Sentence* model.
    owner = models.ForeignKey(User)
    # Worker users that only have write access to the *Translation model.
    workers = models.ForeignKey(User, blank=True)

myprojname/data/models.py看起来像这样:

from django.db import models
from django.contrib.auth.models import User

class Sentence(models.Model):
    text = models.TextField()
    translations = models.ManyToManyField('Translation', blank=True)

class Translation(models.Model):
    text = models.TextField()
    translator = models.ForeignKey(User)

现在Project模型未连接到Sentence模型。

我尝试将其与相关导入相关联,即myprojname/projects/models.py,我做了:

from django.db import models
from django.contrib.auth.models import User
from ..data import Sentence

class Project(models.Model):
    # ...
    sentences = models.ForeignKey(Sentence)

但它在ValueError中返回了Python2.7

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper

    fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
    from ..data import Sentence
ValueError: Attempted relative import beyond toplevel package

Python3中,同样的错误:

Traceback (most recent call last):
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise

    raise value.with_traceback(tb)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
    from ..data import Sentence
ValueError: attempted relative import beyond top-level package

当我尝试在Python3中进行非亲属导入时:

from django.db import models
from django.contrib.auth.models import User
from myprojname.data import Sentence

class Project(models.Model):
    # ...
    sentences = models.ForeignKey(Sentence)

我明白了:

Traceback (most recent call last):
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run

    autoreload.raise_last_exception()
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/alvas/.virtualenvs/payer/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
    from myprojname.data import Sentence
ImportError: No module named 'myprojname.data'

如何连接不同应用的模型?

被修改

当我在from data import Sentence中使用myprojname.project.models.py时,它会抛出ImportError

Traceback (most recent call last):
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)

  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/home/alvas/.virtualenvs/statnlp/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/alvas/data-project/myprojname/project/models.py", line 5, in <module>
    from data import Sentence
ImportError: cannot import name 'Sentence'

1 个答案:

答案 0 :(得分:2)

您的导入应为from data.models import Sentence,而不是from myprojname.data import Sentence