Skip to content Skip to sidebar Skip to footer

How Do You Access Models From Other Installed Apps In Django When In The Same Subdirectory?

I have the following structure for my project: myproject/ |-- myproject/ | |-- __init__.py | |-- settings.py | |-- urls.py | |-- wsgi.py |-- apps/ | |-- dashboard/ | |

Solution 1:

from .data.models is an import relative to the current directory where that file exists, e.g., it would be looking for a data module directory inside your dashboard directory. If you want to refer to the data module relative to your dashboard module you should be able to use

from ..data.modelsimportFoo

Using absolute imports depends on your PYTHONPATH environment variable. You can play around with it by adjusting sys.path in a Python REPL.

Solution 2:

From the top of my head I can advise a few things:

  1. Check your __init__.py files if they are not empty you'll have access to only whats imported in them.
  2. Check if you are not running into circular imports - if in dashboard.models you import from data.models when it imports from dashboard.models.
  3. from apps.data import models or from apps.data.models import ModelName seems the right way to go - knowing this look for solutions.

Post a Comment for "How Do You Access Models From Other Installed Apps In Django When In The Same Subdirectory?"