解耦 django 应用程序 - 布局项目的最佳实践

发布于 2024-08-16 22:07:58 字数 427 浏览 4 评论 0原文

我正在开发一个具有多个应用程序的项目,并且希望包含一个用于新闻报道的新闻应用程序。

但是,我想将新闻报道链接到自定义应用程序中的对象,但使用开源新闻应用程序来运行新闻。

目前,我只是破解了所选的新闻应用程序以添加与我的模型的外键关系。

即一个小部件应用程序,带有一个小部件模型,

然后是一个新闻应用程序,其条目模型直接链接到我的小部件模型

有没有更好的方法来做到这一点?因为如果我想用最新版本更新新闻应用程序,它显然会覆盖我的黑客。

我可以从我的自定义模型获得链接,但工作流程实际上应该是

  1. 添加新闻文章
  2. 选择一个小部件将其链接到

  1. 添加新闻文章,保存
  2. 查找小部件将其链接到链接
  3. 回新闻文章

I'm working on a project that has several apps, and want to include a news app for news stories.

However, I'd like to link news stories to objects in my custom app, but use an open source news app to run the news.

At the moment I've simply hacked the chosen news app to add in a ForeignKey relationship with my model.

i.e. a widgets app, with a widget model

then a news app with the entry model linked directly to my widget model

Is there a better way to do this? because if I want to update the news app with the latest version of it, it'll obviously overwrite my hack.

I could have the link from my custom model, but the workflow should really be

  1. Add news article
  2. choose a widget to link it to

NOT

  1. Add a news article, save
  2. Find the widget to link it to
  3. Link back to the news article

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

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

发布评论

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

评论(1

甜味超标? 2024-08-23 22:07:58

我认为您可以拥有一个继承外部应用程序中的模型的模型。大致意思是:

MyNewsArticle(ExternalAppNewsArticle):
    object = models.ForeignKey(MyObject)

只要您添加内容而不是删除内容,这应该可行。如果外部应用程序的模型声明了任何自定义管理器,您必须小心,因为默认情况下 Django 不会继承它们。您可能需要在您自己的模型中再次声明它们。

I think you could have a model which inherits from the model in the external app. Something along the lines of:

MyNewsArticle(ExternalAppNewsArticle):
    object = models.ForeignKey(MyObject)

As long as you're adding things rather than dropping things, this should work. You have to be careful if the model from the external app has any Custom Managers declared though, because by default Django will not inherit them. You might need to declare them again in your own model.

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