Django 1.7 冲突模型

问题描述 投票:0回答:3

我将应用程序安装在“project/apps/myapp”文件夹中。 apps 和 myapp 文件夹都有 init.py 文件(没有其中任何一个都会出现模块丢失错误)。现在我有错误:

Exception Type: RuntimeError at /
    Exception Value: Conflicting 'person' models in application 'resume': <class
 'apps.resume.models.Person'> and <class 'resume.models.Person'>.

Django 使用两个不同的路径导入相同的模型。我该如何解决它?

完整错误日志:

Traceback:
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  98.                 resolver_match = resolver.resolve(request.path_info)
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  343.             for pattern in self.url_patterns:
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  372.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  366.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/home/voxa/django/FortyTwoTestTask/fortytwo_test_task/urls.py" in <module>
  4. from resume import views
File "/home/voxa/django/FortyTwoTestTask/apps/resume/views.py" in <module>
  4. from resume.models import Person
File "/home/voxa/django/FortyTwoTestTask/apps/resume/models.py" in <module>
  3. class Person(models.Model):
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/db/models/base.py" in __new__
  285.         new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
File "/home/voxa/.virtualenvs/42-test/local/lib/python2.7/site-packages/django/apps/registry.py" in register_model
  213.                 (model_name, app_label, app_models[model_name], model))

Exception Type: RuntimeError at /
Exception Value: Conflicting 'person' models in application 'resume': <class 'apps.resume.models.Person'> and <class 'resume.models.Person'>.
django django-models python-module
3个回答
12
投票

不必导入所有项目,然后导入应用程序,然后导入应用程序内的模块,只需导入项目内的应用程序,然后导入模块。

而不是

from webproject.app import model

使用

from app import model

from app.models import Staffs

5
投票

我认为这个错误报告(事实证明这是一个功能)与您的问题有关。

对我来说,问题是通过仅从

resume.models
导入解决的,而不是从
apps.resume.models
导入。因此,在您的项目中搜索
"from apps."
并将其替换。

(对我来说,删除

__init__.py
或更改
PYTHONPATH
会导致其他问题,我想这很常见。)


0
投票

这个问题已经有一个世纪了,但这是我的解决方案,找到它真的很痛苦。 给定这个项目树:

repo
├── app
│   ├── __init__.py
│   ├── project
│   │   ├── __init__.py
│   │   ├── asgi.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   ├── utils.py
│   │   └── wsgi.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── admin
│   │   │   ├── __init__.py
│   │   │   └── ...
│   │   ├── apps.py
│   │   ├── models
│   │   │   ├── __init__.py
│   │   │   ├── mymodel.py
.   .   .   ...

除了

core
中包含
PYTHONPATH
文件夹之外,还有我正在做的
from ..models import MyModel
的地方。

显然,使用相对导入(

..models
)以某种方式从存储库文件夹生成路径
app
,这是
app.core.models

运行测试时,它有时会在两个路径中引用相同的模型来假设重复:

<app.core.models.MyModel>
<core.models.MyModel>
,所以我选择使用
core.models
而不是
..models

© www.soinside.com 2019 - 2024. All rights reserved.