Django 1.7 conflicting models

感情迁移 提交于 2019-12-28 16:34:08

问题


I install my application in "project/apps/myapp" folder. Both apps and myapp folders have init.py files(Without any of them there is module missing error). Now I've the error:

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

Django import the same model with two different pathes. How can I fix it?

Full error log:

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'>.

回答1:


Instead of importing the all project then the app then the module inside the app just import the app which is inside the project then the module.

Instead of

from webproject.app import model

Use

from app import model

or

from app.models import Staffs



回答2:


I think this bug report (turns out it's a feature) is related to your problem.

For me the problem was resolved by importing from resume.models only, rather than apps.resume.models. So search for "from apps." in your project and replace it.

(For me, removing __init__.py or changing the PYTHONPATH caused other problems, I imagine that's common.)



来源:https://stackoverflow.com/questions/26591399/django-1-7-conflicting-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!