Django 1.7 conflicting models
Asked Answered
M

4

27

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'>.
Melaniemelanin answered 27/10, 2014 at 15:46 Comment(5)
Do you have the "resume" app both at project/resume and project/apps/resume?Barnard
no only "project/apps/resume"Melaniemelanin
Can you do from apps.resume.models import Person as Person1; from resume.models import Person as Person2; print Person1.__file__; print Person2.__file__ and post the results here?Barnard
Same issue for me, did you solve it?Wonderful
I solve this problem as Wangolo Djoel described in his answerMelaniemelanin
T
12

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
Thigpen answered 26/3, 2015 at 6:57 Comment(0)
D
5

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

Discalced answered 23/2, 2015 at 14:30 Comment(0)
H
0

This issue has a century, but here goes my solution that was really painful to find. Given this project tree:

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

Besides having the core folder included in the PYTHONPATH, there were places where I was doing from ..models import MyModel.

Apparently, using relative imports (..models) was in some way generating the path from the repo folder app, this is app.core.models.

When running the tests, it assumed a duplication through sometimes refering the the same model in both paths: <app.core.models.MyModel> and <core.models.MyModel>, so I opted for using core.models instead of ..models.

Hetaerism answered 29/8, 2023 at 13:53 Comment(0)
I
0

I had similar problem which was happening just in tests.

After hours of search and try/failure, finally in __init__.py of models folder (where I have a core.py file) I changed

from .core import *

to

from appName.models.core import *

and problem is solved now.

I hope it helps you too.

Itin answered 8/6, 2024 at 14:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.