How do you access models from other installed apps in Django when in the same subdirectory?

后端 未结 2 1522
借酒劲吻你
借酒劲吻你 2021-01-29 04:17

I have the following structure for my project:

myproject/
|-- myproject/
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   |-- wsgi.py
|-- apps/
|   |-         


        
2条回答
  •  悲哀的现实
    2021-01-29 04:46

    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.models import Foo
    

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

提交回复
热议问题