Django子应用程序和模块结构 [英] Django sub-applications & module structure

查看:121
本文介绍了Django子应用程序和模块结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Django应用程序,这是一个大型系统,需要多个子应用程序才能保持整洁。因此,我有一个顶级目录,它是一个Django应用程序(因为它有一个空的 models.py 文件)和多个子目录,它们本身也是应用程序。 / p>

我之所以这样安排我的应用程序,是因为子应用程序是分开的,但是它们永远不会在父应用程序之外单独使用。因此,单独分发它们是没有意义的。



在安装我的应用程序时,设置文件必须包含以下内容:

  INSTALLED_APPS =(
...
'myapp',
'myapp.subapp1',
'myapp.subapp2 ',
...

...这显然不是最佳选择。这也带来了令人讨厌的结果,即要求所有子应用程序都使用其内部名称(即 subapp1 subapp2 等)。例如,如果要重置subapp1的数据库表,则必须输入:

  python manage.py reset subapp1 

这很烦人,特别是因为我有一个名为 core的子应用-当我的应用程序安装在用户项目中时,很可能与另一个应用程序的名称冲突。

解决方案

您是用正确的方式来做这些事情?因为django本身就是这样做的。例如,管理应用程序已在 INSTALLED_APPS 中注册为 django.contrib.admin ,但是要重置它,您必须使用 manage.py reset admin ,实际上, manage.py reset django.contrib.admin 不起作用



它可能被视为django中的bug ...



但是,您不必担心名称冲突,因为您应该始终在 virtualenv 环境中运行django,该环境应与其余的python安装隔离。与在普通的python安装上运行django相比,这是一个功能更强大,更灵活的解决方案。例如,更多信息,请点击此处: http://mathematism.com/ 2009 / jul / 30 / presentation-pip-and-virtualenv /


I am developing a Django application, which is a large system that requires multiple sub-applications to keep things neat. Therefore, I have a top level directory that is a Django app (as it has an empty models.py file), and multiple subdirectories, which are also applications in themselves.

The reason I have laid my application out in this way is because the sub-applications are separated, but they would never be used on their own, outside the parent application. It therefore makes no sense to distribute them separately.

When installing my application, the settings file has to include something like this:

INSTALLED_APPS = (
    ...
    'myapp',
    'myapp.subapp1',
    'myapp.subapp2',
    ...
)

...which is obviously suboptimal. This also has the slightly nasty result of requiring that all the sub-applications are referred to by their "inner" name (i.e. subapp1, subapp2 etc.). For example, if I want to reset the database tables for subapp1, I have to type:

python manage.py reset subapp1

This is annoying, especially because I have a sub-app called core - which is likely to conflict with another application's name when my application is installed in a user's project.

Am I doing this completely wrongly, or is there away to force these "inner" apps to be referred to by their full name?

解决方案

You are doing it the right way, since django itself does it that way. The admin app for instance is registered in INSTALLED_APPS as django.contrib.admin, but to reset it you have to use manage.py reset admin, and indeed, manage.py reset django.contrib.admin does not work.

It could be considered as a bug in django...

However, you should not be concerned by name conflicts, because you should always run django inside a virtualenv environment, isolated from the rest of the python installation. This is an immensely more powerful and flexible solution than running django on an ordinary python installation. More info, for instance, here: http://mathematism.com/2009/jul/30/presentation-pip-and-virtualenv/

这篇关于Django子应用程序和模块结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆