我正在尝试在我的Django 1.7项目中自定义应用程序名称,如此处的文档中所示:https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-authors
我认为我已经按照规定完成了,但现在我无法将模型从app导入应用,例如:
ImportError: No module named comune.models
我构建了我的项目,以便应用程序位于目录树的1层深处,如下所示:
project_root
├── apps
│ ├── allegato
│ ├── calendario
│ ├── cliente
│ ├── commessa
│ ├── comune
│ ├── laboratorio
│ ├── recapito
│ ├── spedizione
│ ├── strumento
│ ├── taratura
│ └── tecnico
├── locale
├── media
├── project
│ ├── . . . (my project directory)
├── static
└── templates
我想这可能是导致这个问题的原因,但我修复它的尝试还没有达到目的。
以下是我计划为每个应用做的事情,我展示了一个名为" cliente" 的应用的具体案例(&#34)我到目前为止唯一修改过的一个):
__初始化__。PY
default_app_config = 'apps.cliente.apps.ClienteConfig'
apps.py
from django.apps import AppConfig
class ClienteConfig(AppConfig):
name = 'apps.cliente'
verbose_name = "Clienti e prodotti cliente"
回溯:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/seether/.virtualenvs/mytime/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/seether/Projects/mytime/apps/cliente/models.py", line 6, in <module>
from apps.comune.models import Comune, Cap
ImportError: No module named comune.models
我在这里看错了什么?
答案 0 :(得分:0)
好的,似乎建议的文件名&#34; apps.py&#34;以某种方式欺骗这种奇怪的行为。也许是我的应用程序目录名为&#34; apps&#34;同样(如Two Scoops of Django中所述)会产生混淆。
只需将 apps.py 文件重命名为 config.py 并相应地修复 __ init __。py :
<强> __初始化__。PY 强>
default_app_config = 'apps.cliente.config.ClienteConfig'
诀窍。