[Django]-Django 1.7 conflicting models

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
πŸ‘€Wangolo Joel

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

πŸ‘€Mark

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.

Leave a comment