django - 在 Django 1.9 中的应用程序之间导入模型

标签 django python-2.7 django-models

我刚刚在 Django 1.9 中开始一个新项目,由于某种原因无法将我的模型从一个应用程序导入到另一个应用程序。下面是我的文件结构:

├── apps
    ├── __init__.py
    ├── main
        |--__init__.py
        |--models.py
    └── contacts
        |--__init__.py
        |--models.py
├── config
    ├── __init__.py
    ├── settings.py
    └── wsgi.py

还有我在 settings.py 中的 INSTALLED_APPS
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'apps.main',
    'apps.contacts',
]

出于某种原因,自从升级到 1.9,这个导入
from apps.main.models import BaseUser

在contacts/models.py 中返回以下错误。
Traceback (most recent call last):   
File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)   
File "/Users/kendallcole/.virtualenvs/trickledowncrm/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()   
File "/Users/kendallcole/.virtualenvs/trickledowncrm/lib/python2.7/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()   
File "/Users/kendallcole/.virtualenvs/trickledowncrm/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)   
File "/Users/kendallcole/.virtualenvs/trickledowncrm/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)   
File "/Users/kendallcole/.virtualenvs/trickledowncrm/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)   
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)   
File "/Users/kendallcole/Desktop/trickledowncrm/apps/contacts/models.py", line 4, in <module>
    from apps.main.models import BaseUser 
ImportError: No module named main.models

最佳答案

我使用 Django apps registry described here 解决了这个问题.

我试图从另一个名为 warehouse 的应用程序导入模型:

from warehouse.models import Member

这给了我和你一样的错误。我通过将模型分配给变量而不是尝试导入 warehouse 来解决它楷模:
from django.apps import apps

class ModelFromAnotherApp(models.Model):
    ...
    def method_using_model_from_another_app:
        member = apps.get_model('warehouse.Member')
        x = members.objects.filter(...)
    ...

关于django - 在 Django 1.9 中的应用程序之间导入模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050923/

相关文章:

python - 在 ManyToManyField 中使用交叉引用时出现 Django NameError

python - Django 中的虚拟过滤器

python - 基于 URL 的数据库路由

python - 如何为 Celery 任务添加自定义 ID 以便稍后撤销任务?

python - 使用 Python 提取 Flipkart.com 产品 'price' 和产品 'title'

python - Python 对函数参数和操作数的求值顺序是否确定(+ 它记录在何处)?

python - 使用 subprocess.check_output() 时出现 OSError 异常 '[Errno 2] No such file or directory'

使用 get_queryset 的 Django ListView 分页

django - 更好地扩展或替代 Django 用户模型?

python - 获取 Django 中每个页面用户类型的值