如何用抽象模型组织 Django 项目
我有几个模型:“文章、视频、博客文章、新闻、商品”。每个都在自己的应用程序中。
它们基本上都是相同的模型,每个模型都有一些额外的字段。但每个共享大约 15 个字段。我正在使用抽象基类。我正在尝试找出应该如何为此进行组织。我当前的设置是这样的:
apps/
abstract_models.py
abstract_templatetags.py
abstract_forms.py
articles/
models.py
...
videos/
models.py
...
blogs/
...
虽然我知道这不是一个好方法,但我只是不确定将所有共享的信息放在哪里。我一直在这样做,然后每个应用程序只需对表单或模型进行子类化并进行本地修改。由于与整体相比,它们只是少量的更改,因此我认为抽象类是正确的选择,但我可能是错的。
它们共享如此多的结构,但出于显而易见的原因,我想让它们保持独立的应用程序。但我想把它清理一下。
任何想法将不胜感激。
I have a few models: 'Article, Video, BlogPost, News, Commodity'. Each are in their own application.
They all are basically the same models with a few extra fields on each. But each share about 15 fields. I'm using an abstract base class. I'm trying to figure out how I should do organization for this. My current setup is like this:
apps/
abstract_models.py
abstract_templatetags.py
abstract_forms.py
articles/
models.py
...
videos/
models.py
...
blogs/
...
While I know this isnt a good way, I'm just not sure where to put all the information that is shared. I've been doing like this, then per app just subclassing the Form or the Model and making the local modifications. Since they are just a small amount of changes vs the whole picture I think that abstract class is the way to go, but I may be wrong.
They share so much structure, but for obvious reasons I would like to keep them separate apps. But I would like to clean it up a bit.
Any thoughts would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我设置了一个可以在不同项目中使用的应用程序,并将其命名为“工具”。在
tools
中,我有基本的基本模型,我倾向于在项目中重用它们并在需要时导入它。例如,我在
tools/models.py
中有一个CreatedModifiedModel
,它添加了创建和修改时间的字段,以及进行创建和修改的用户。定义一次后,我可以简单地执行以下操作:
您可以创建一个名为
base
或core
或tools
的应用程序,然后放置所有抽象类在那里,以帮助保持清洁并使其在将来可以重复使用。I setup an app that I can use across different projects and call it
tools
. Intools
I have my basic base models that I tend to reuse across projects and import it where needed.For example, I have a
CreatedModifiedModel
intools/models.py
which adds fields for creation and modification time, as well as the user who did the creating and modifying.After being defined once, I can simply do:
You could create a single app called
base
orcore
ortools
and then put all your abstract classes in there, to help keep it clean and make it reusable in the future.Pinax 项目 与组有类似的东西。他们创建了自己的基类以及将其扩展到应用程序的基类。
组织起来似乎很棒。你可以在 github 上查看他们的源代码。
The Pinax project have something similar with groups. They have made a their base class and the ones that extend it into an application.
That seems like a great to organize it. You can take a look at their source code at github.
在我的公司,我们通常将 Django 项目的所有抽象组件组织到一个名为
common
的应用程序中。它的结构与普通 Django 应用程序一样,只是它没有 url,也没有具体的模型。它像任何其他应用程序一样安装在settings.py
中。它也是放置任何全局使用的 mixins 或异常的好地方。
有了这个应用程序,我可以从任何其他应用程序引用它,例如:
article/models.py:
article/views.py:
atricle/templates/article/detail.html:
At my firm, we typically organize all of the abstract components of my Django projects into an app called
common
. It is structured as a normal Django app, except that it has no urls, and no concrete models. It gets installed insettings.py
, just like any other app.It's also a great place to stick any globally-used mixins or exceptions.
With this app in place, I can reference it from any other app, such as in:
article/models.py:
article/views.py:
atricle/templates/article/detail.html: