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

Import file from another app in 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):
    ...

我从另一个名为 website

的应用创建了一个名为 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

这是我的代码结构:

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

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