如何用抽象模型组织 Django 项目

发布于 2024-08-07 11:37:17 字数 633 浏览 9 评论 0原文

我有几个模型:“文章、视频、博客文章、新闻、商品”。每个都在自己的应用程序中。

它们基本上都是相同的模型,每个模型都有一些额外的字段。但每个共享大约 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 技术交流群。

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

发布评论

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

评论(3

作妖 2024-08-14 11:37:17

我设置了一个可以在不同项目中使用的应用程序,并将其命名为“工具”。在 tools 中,我有基本的基本模型,我倾向于在项目中重用它们并在需要时导入它。

例如,我在 tools/models.py 中有一个 CreatedModifiedModel,它添加了创建和修改时间的字段,以及进行创建和修改的用户。

定义一次后,我可以简单地执行以下操作:

from tools.models import CreatedModifiedModel    

class Widget(CreatedModifiedModel):
    # comes with my four fields automatically

您可以创建一个名为 basecoretools 的应用程序,然后放置所有抽象类在那里,以帮助保持清洁并使其在将来可以重复使用。

I setup an app that I can use across different projects and call it tools. In tools I have my basic base models that I tend to reuse across projects and import it where needed.

For example, I have a CreatedModifiedModel in tools/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:

from tools.models import CreatedModifiedModel    

class Widget(CreatedModifiedModel):
    # comes with my four fields automatically

You could create a single app called base or core or tools and then put all your abstract classes in there, to help keep it clean and make it reusable in the future.

一身骄傲 2024-08-14 11:37:17

Pinax 项目 与组有类似的东西。他们创建了自己的基类以及将其扩展到应用程序的基类。

/apps
    /group
        base.py
        ...
    /projects
        models.py
        ...

组织起来似乎很棒。你可以在 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.

/apps
    /group
        base.py
        ...
    /projects
        models.py
        ...

That seems like a great to organize it. You can take a look at their source code at github.

ゃ人海孤独症 2024-08-14 11:37:17

在我的公司,我们通常将 Django 项目的所有抽象组件组织到一个名为 common 的应用程序中。它的结构与普通 Django 应用程序一样,只是它没有 url,也没有具体的模型。它像任何其他应用程序一样安装在 settings.py 中。

它也是放置任何全局使用的 mixins 或异常的好地方。

my_prjoject/
  common/
    models.py <- abstract models only
    templates/
      common/
        base.html
    views.py
    mixins.py
    exceptions.py
  article/
    models.py
    views.py
    urls.py
    templates/
      atricle/
        detail.html
  ...

注意:以单数命名您的应用程序。如果你不这样做,鸟儿就会攻击你。

有了这个应用程序,我可以从任何其他应用程序引用它,例如:

article/models.py:

from common import models as common_models


class Article(common_models.BaseModel):
    additional_field = models.CharField(max_length=10) # Whatever

article/views.py:

from common import views as common_views


class Detail(common_views.DetailBase):
    template_name = "article/detail.html"

atricle/templates/article/detail.html:

{% extends 'common/base.html' %}

{% block base %}

<h1>If you downvote this answer, I will fight you</h1>

{% endblock base%}

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 in settings.py, just like any other app.

It's also a great place to stick any globally-used mixins or exceptions.

my_prjoject/
  common/
    models.py <- abstract models only
    templates/
      common/
        base.html
    views.py
    mixins.py
    exceptions.py
  article/
    models.py
    views.py
    urls.py
    templates/
      atricle/
        detail.html
  ...

Note: name your apps in the singular. If you don't birds will attack you.

With this app in place, I can reference it from any other app, such as in:

article/models.py:

from common import models as common_models


class Article(common_models.BaseModel):
    additional_field = models.CharField(max_length=10) # Whatever

article/views.py:

from common import views as common_views


class Detail(common_views.DetailBase):
    template_name = "article/detail.html"

atricle/templates/article/detail.html:

{% extends 'common/base.html' %}

{% block base %}

<h1>If you downvote this answer, I will fight you</h1>

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