【问题标题】:django directory structure of module for commands命令模块的django目录结构
【发布时间】:2014-04-19 17:35:40
【问题描述】:

在 django 1.6 中,我有一个用于对数据库进行分析的命令,带有一组函数。

├── management
│   ├── __init__.py
│   └── commands
│       ├── __init__.py
│       ├── analysis.py

我喜欢将 analysis.py 分解为一组文件(可能是一个模块......)。 用于 django 命令的模块的正确目录结构是什么?

也许像这样...?

├── management
│   ├── __init__.py
│   ├── analysis_module
│   └── commands
│       ├── __init__.py
│       ├── analysis.py

analysis_module 的测试,analysis_module 测试的正确 ubication 是什么?

【问题讨论】:

  • 对我来说似乎很合理。 This answer 暗示 management 目录用的不多,所以我怀疑把你的通用代码放在那里有什么风险。
  • 如果分析模块可能是可重用的,我会将其完全移出您的应用程序,然后将其导入您的 analysis.py 命令。我喜欢尽可能将 django 应用程序/模块和非 django/python 模块分开
  • @TimmyO'Mahony 是的,我正在考虑像您评论这样的解决方案,我将其添加为答案。但我不确定是否是正确的导入方式...

标签: django django-models django-commands


【解决方案1】:

一个选项,将代码模块从django项目中取出

├── analysis_module
├── test
├── django_project
│   ├── app
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── management
│   │   │   ├── __init__.py
│   │   │   └── commands
│   │   │       ├── __init__.py
│   │   │       └── analysis.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── urls.pyc
│   │   └── views.py
│   └── django_proyect
│       ├── __init__.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py

但为此,我需要将模块添加到 sys.path 的路径中,可能在 manage.py

# manage.py
sys.path.append(os.path.abspath(__file__ + '/../../'))

并从分析命令导入

# analysis.py
import analysis_module

有了这个,我可以单独对 analysis_module 进行测试。

【讨论】:

    猜你喜欢
    • 2012-10-09
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多