Django 子应用程序和模块结构

发布于 2024-08-28 14:58:40 字数 666 浏览 6 评论 0原文

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

我以这种方式布局应用程序的原因是因为子应用程序是分开的,但它们永远不会在父应用程序之外单独使用。因此,单独分发它们是没有意义的。

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

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

...这显然不是最佳的。这也有一个稍微令人讨厌的结果,即要求所有子应用程序都通过其“内部”名称(即 subapp1subapp2 等)来引用。例如,如果我想重置 subapp1 的数据库表,我必须输入:

python manage.py reset subapp1

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

我这样做是否完全错误,或者是否有办法强制这些“内部”应用程序以全名引用?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-09-04 14:58:40

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

它可以被认为是 django 中的一个错误...

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文