问题:何时在Django中创建新应用(使用startapp)?

我已经为此进行了搜索,但是在Django定义为“应用程序”方面仍然遇到麻烦。

我是否应该为站点中的每个功能创建一个新应用,即使该应用使用了主项目中的模型?

你们对何时拆分新应用程序以及何时将功能与“主项目”或其他应用程序保持在一起有很好的经验法则吗?

I’ve googled around for this, but I still have trouble relating to what Django defines as “apps”.

Should I create a new app for each piece of functionality in a site, even though it uses models from the main project?

Do you guys have good rule of thumb of when to split off a new app, and when to keep functionality together with the “main project” or other apps?


回答 0

詹姆斯·本内特(James Bennett)提供了一系列精彩的幻灯片,介绍了如何在Django中组织可重复使用的应用程序。

James Bennett has a wonderful set of slides on how to organize reusable apps in Django.


回答 1

我更喜欢将Django应用程序视为可重用的模块或组件,而不是“应用程序”。

这有助于我将某些功能相互封装和解耦,如果我决定与整个社区共享特定的“应用”,则可以提高可重用性,并具有可维护性。

我的一般方法是将特定功能或功能集存储到“应用程序”中,就像我要公开发布它们一样。这里最困难的部分是弄清楚每个存储桶有多大。

我使用的一个好技巧是想象如果我的应用程序公开发布后将如何使用。这通常鼓励我缩水缩水,更清楚地定义其“目的”。

I prefer to think of Django applications as reusable modules or components than as “applications”.

This helps me encapsulate and decouple certain features from one another, improving re-usability should I decide to share a particular “app” with the community at large, and maintainability.

My general approach is to bucket up specific features or feature sets into “apps” as though I were going to release them publicly. The hard part here is figuring out how big each bucket is.

A good trick I use is to imagine how my apps would be used if they were released publicly. This often encourages me to shrink the buckets and more clearly define its “purpose”.


回答 2

这是2008年9月6日更新的演示文稿。

DjangoCon 2008:可重用应用@ 7:53

幻灯片:Reusable_apps.pdf

取自幻灯片

这应该是它自己的应用程序吗?

  • 它与应用程序的关注点完全无关吗?
  • 它与我正在做的其他事情正交吗?
  • 我在其他站点上是否需要类似的功能?

如果其中任何一个是“是”?然后最好将其分解为一个单独的应用程序。

Here is the updated presentation on 6 September 2008.

DjangoCon 2008: Reusable Apps @7:53

Slide: Reusable_apps.pdf

Taken from the slide

Should this be its own application?

  • Is it completely unrelated to the app’s focus?
  • Is it orthogonal to whatever else I’m doing?
  • Will I need similar functionality on other sites?

If any of them is “Yes”? Then best to break it into a separate application.


回答 3

我倾向于为每个逻辑上分离的模型集创建新的应用程序。例如:

  • 用户资料
  • 论坛帖子
  • 博客文章

I tend to create new applications for each logically separate set of models. e.g.:

  • User Profiles
  • Forum Posts
  • Blog posts

回答 4

我遵循的规则是,如果我想在其他项目中重用该功能,则它应该是一个新应用。

如果需要对项目中的模型有深入的了解,则将其与模型结合在一起可能会更有凝聚力。

The rule I follow is it should be a new app if I want to reuse the functionality in a different project.

If it needs deep understanding of the models in your project, it’s probably more cohesive to stick it with the models.


回答 5

我在网上发现的这个问题的两个最佳答案是:

  1. 其他答案中也提到了可重复使用的应用程序对话(幻灯片)(视频)。Bennett是Django的作者和撰稿人,他定期发布供他人使用的应用程序,并对许多小型应用程序有很强的见解。
  2. Doordash的《面向Django的技巧》给出了相反的建议,并表示在从许多单独的应用程序开始后,他们迁移到了一个应用程序。他们在应用之间的迁移依赖关系图中遇到了问题。

两种消息来源都同意您在以下情况下应创建一个单独的应用程序:

  • 如果您打算在另一个Django项目中重用您的应用程序(尤其是如果您打算将其发布以供其他人重用)。
  • 该应用程序与另一个应用程序之间的依赖关系很少或没有。在这里,您可能可以想象将来有一个应用程序将作为其自己的微服务运行。

The two best answers to this question I’ve found around the web are:

  1. The Reusable Apps Talk (slides)(video) also mentioned in other answers. Bennett, the author and Django contributor, regularly publishes apps for others to use and has a strong viewpoint towards many small apps.
  2. Doordash’s Tips for Django at Scale which gives the opposite advice and says in their case they migrated to one single app after starting with many separate apps. They ran into problems with the migration dependency graph between apps.

Both sources agree that you should create a separate app in the following situations:

  • If you plan to reuse your app in another Django project (especially if you plan to publish it for others to reuse).
  • If the app has few or no dependencies between it and another app. Here you might be able to imagine an app running as its own microservice in the future.

回答 6

一个“应用程序”可能有很多不同的东西,这一切真的都让人联想到。例如,假设您正在构建博客。您的应用程序可以是整个博客,也可以有一个“管理员”应用程序,一个用于所有公共视图的“站点”应用程序,一个“ rss”应用程序,一个“服务”应用程序,以便开发人员可以在其博客中与该博客进行交互自己的方式等

我个人将使博客本身成为该应用程序,并扩展其中的功能。然后可以很轻松地在其他网站中重复使用该博客。

Django的优点在于,它将目录树中任何级别的所有models.py文件都识别为包含Django模型的文件。因此,将功能分解为“应用程序”本身中较小的“子应用程序”不会带来任何困难。

An ‘app’ could be many different things, it all really comes down to taste. For example, let’s say you are building a blog. Your app could be the entire blog, or you could have an ‘admin’ app, a ‘site’ app for all of the public views, an ‘rss’ app, a ‘services’ app so developers can interface with the blog in their own ways, etc.

I personally would make the blog itself the app, and break out the functionality within it. The blog could then be reused rather easily in other websites.

The nice thing about Django is that it will recognize any models.py file within any level of your directory tree as a file containing Django models. So breaking your functionality out into smaller ‘sub apps’ within an ‘app’ itself won’t make anything more difficult.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。