Django 管理器代码应该放在哪里?

发布于 2024-08-14 05:34:24 字数 179 浏览 4 评论 0原文

这是一个非常简单的 django 模式问题。我的管理器代码通常位于 models.py 中,但是当 models.py 非常大时会发生什么?是否有其他替代模式可以让您的管理器代码存在于 models.py 中以实现可维护性并避免循环导入?

可能有人会问为什么 models.py 如此庞大,但我们假设它的大小和实用范围是合理的。

This is a pretty simple django patterns question. My manager code usually lives in models.py, but what happens when models.py is really huge? Is there any other alternative pattern to letting your manager code live in models.py for maintainability and to avoid circular imports?

A question may be asked as to why models.py is so huge, but let's just assume it's size and breadth of utility is justified.

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

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

发布评论

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

评论(4

明月松间行 2024-08-21 05:34:25

我更喜欢将模型保留在 models.py 中,将管理器保留在 manager.py 中(表单在 forms.py 中),所有这些都在同一个应用程序中。对于更通用的管理器,如果它们可以重用于其他应用程序,我更愿意将它们保留在 core.managers 中。在我们一些带有 models/modelname.py 的大型应用程序中,它将包含一个管理器和模型代码,这看起来不错。

I prefer to keep my models in models.py and managers in managers.py (forms in forms.py) all within the same app. For more generic managers, I prefer to keep them in core.managers if they can be re-used for other apps. In some of our larger apps with models/modelname.py that will contains a manager and the model code which doesn't seem bad.

北风几吹夏 2024-08-21 05:34:25

对于大量模型,最好的选择是使用 django 模块来发挥你的优势,并简单地创建一个名为 models 的文件夹。将旧的 models.py 移动到此 models 文件夹中,并将其重命名为 __init__.py。这将允许您将每个模型分成该模型文件夹内更具体的文件。

然后,您只需将每个模型导入到 __init__.py 的命名空间中。

因此,例如,您可能希望将其分成:

yourapp/
    models/
        __init__.py # This file should import anything from your other files in this directory
        basic.py # Just an example name
        morespecificmodels.py # Just an example name
        managers.py # Might want to separate your manager into this

那么您的 __init__.py 可以是:

from basic import * # You should replace * with each models name, most likely.
from managers import YourManager # Whatever your manager is called.

这是当我的模型文件变得巨大时我使用的结构,但是我尝试将其分成尽可能多地使用更多可插拔的应用程序 - 所以我很少使用它。

希望这有帮助。

Your best bet with a large set of models is to use django modules to your advantage, and simply create a folder named models. Move your old models.py into this models folder, and rename it __init__.py. This will allow you to then separate each model into more specific files inside of this model folder.

You would then only need to import each model into your __init__.py's namespace.

So, for instance, you might want to separate it into:

yourapp/
    models/
        __init__.py # This file should import anything from your other files in this directory
        basic.py # Just an example name
        morespecificmodels.py # Just an example name
        managers.py # Might want to separate your manager into this

Then your __init__.py can just be:

from basic import * # You should replace * with each models name, most likely.
from managers import YourManager # Whatever your manager is called.

This is the structure that I use when my model files get huge, however I try to separate things into more pluggable apps as often as possible - so this is rarely used by me.

Hope this helps.

心作怪 2024-08-21 05:34:25

我总是将我的放在managers.py中。如果您遇到循环导入问题,请记住:a) 您可以在 self.model 中引用管理器的模型类,b) 您可以在函数内部进行导入。

I always place mine in managers.py. If you have a circular import issue remember that a) You can reference the model class for a manager at self.model, and b) You can do imports inside of functions.

甜味拾荒者 2024-08-21 05:34:25

我在构建 Django 应用程序时所做的是创建一个 [modelname].py 文件,其中仅包含特定的模型代码、管理器代码,有时还有表单代码,并使用 __init__.py 文件将所有内容导入模型目录中。这至少帮助我保持它的可控性。

What I did when building Django apps was to create a [modelname].py file with just the specific model code, manager code and sometimes form code and used an __init__.py file to import then all in the models directory. This helped me atleast in keeping it managable.

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