从Django 1.10中的另一个应用程序导入文件

时间:2016-10-15 01:30:47

标签: python django django-1.10

我正在尝试从其他应用导入文件,我收到此错误:

Unhandled exception in thread started by <function wrapper at 0x7fe7bc281d70>
Traceback (most recent call last):
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/carlos/.envs/lafam/local/lib/python2.7/site-packages/django/apps/config.py", line 199, 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/carlos/www/lafam/apps/maquina/models.py", line 6, in <module>
    from apps.website.managers import GenericManager
ImportError: No module named website.managers

在settings.py中安装了应用程序

SYSTEM_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

PROJECT_APPS = [
    'apps.website',
    'apps.maquina',
]

INSTALLED_APPS = SYSTEM_APPS + PROJECT_APPS

我的所有应用都在应用文件夹中。我有一个名为“maquina”的应用程序。这是 modals.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from apps.website.managers import GenericManager
from .utils import path_and_rename


class Fabricante(models.Model):
    ...

我从另一个名为网站的应用

创建了一个名为 managers.py 的文件
from django.db import models

class GenericManager(models.Manager):
    def get_or_none(self, *args, **kwargs):
        try:
            return self.get(*args, **kwargs)
        except self.model.DoesNotExist:
            return None
        except self.model.MultipleObjectsReturned:
            return None

我注意到错误就在这一行:

from apps.website.managers import GenericManager

这是我的代码结构:

enter image description here

如何从其他应用导入文件的正确方法?谢谢!

1 个答案:

答案 0 :(得分:1)

设置中的

尝试在BASE_DIR sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))之后添加此行,不要忘记import os import sys