指导大型Django项目的组织工作

时间:2023-01-16 17:53:59

Anyone could recommend a good guide/tutorial/article with tips/guidelines in how to organize and partition a large Django project?

任何人都可以推荐一本关于如何组织和划分大型Django项目的指南/教程/文章。

I'm looking for advices in what to do when you need to start factorizing the initial unique files (models.py, urls.py, views.py) and working with more than a few dozens of entities.

我正在寻找当你需要开始分解初始唯一文件(模型)时该怎么做的建议。py,url。与几十个实体一起工作。

2 个解决方案

#1


38  

Each "application" should be small -- a single reusable entity plus a few associated tables. We have about 5 plus/minus 2 tables per application model. Most of our half-dozen applications are smaller than 5 tables. One has zero tables in the model.

每个“应用程序”都应该很小——一个可重用的实体加上几个相关联的表。每个应用程序模型大约有5个正/负2个表。我们的6个应用程序中的大多数都小于5个表。模型中有0个表。

Each application should be designed to be one reusable concept. In our case, each application is a piece of the overall site; the applications could be removed and replaced separately.

每个应用程序都应该设计为一个可重用的概念。在我们的例子中,每个应用程序都是整个站点的一部分;这些应用程序可以分别删除和替换。

Indeed, that's our strategy. As our requirements expand and mature, we can remove and replace applications independently from each other.

的确,这是我们的策略。随着我们的需求的扩展和成熟,我们可以独立地删除和替换应用程序。

It's okay to have applications depend on each other. However, the dependency has to be limited to the obvious things like "models" and "forms". Also, applications can depend on the names in each other's URL's. Consequently, your named URL's must have a form like "application-view" so the reverse function or the {% url %} tag can find them properly.

让应用程序相互依赖是可以的。但是,依赖关系必须局限于诸如“模型”和“表单”之类的明显的东西。此外,应用程序可以依赖于彼此的URL中的名称。因此,您的命名URL必须具有类似“应用程序视图”的表单,以便反向函数或{% URL %}标记能够正确地找到它们。

Each application should contain it's own batch commands (usually via a formal Command that can be found by the django-admin script.

每个应用程序都应该包含自己的批处理命令(通常是通过django-admin脚本可以找到的正式命令)。

Finally, anything that's more complex than a simple model or form that's shared probably doesn't belong to either application, but needs to be a separate shared library. For example, we use XLRD, but wrap parts of it in our own class so it's more like the built-in csv module. This wrapper for XLRD isn't a proper part of any one application, to it's a separate module, outside the Django applications.

最后,任何比简单的模型或共享的表单更复杂的东西可能不属于任何一个应用程序,但需要是一个单独的共享库。例如,我们使用XLRD,但是在我们自己的类中封装它的部分,所以它更像内置的csv模块。XLRD的包装器不是任何一个应用程序的适当部分,对于它来说,它是Django应用程序之外的一个单独的模块。

#2


10  

I've found it to be helpful to take a look at large open-source Django projects and take note of how that project does it. Django's site has a good list of open-source projects:

我发现,看看大型开源Django项目是有帮助的,并注意到这个项目是如何实现的。Django的站点有一个很好的开源项目列表:

http://code.djangoproject.com/wiki/DjangoResources#Open-SourceDjangoprojects

http://code.djangoproject.com/wiki/DjangoResources Open-SourceDjangoprojects

As does Google (although most of these are smaller add-in template tags and Middleware:

谷歌也一样(尽管其中大部分是较小的插件模板标记和中间件):

http://code.google.com/hosting/search?q=label:django

http://code.google.com/hosting/search?q=label django

Of course, just because one project does it one way does not mean that that way is The Right Way (or The Wrong Way). Some of those projects are more successful than others.

当然,仅仅因为一个项目以一种方式进行,并不意味着这种方式是正确的(或者是错误的)。有些项目比其他项目更成功。

In the end, the only way to really learn what works and doesn't work is to try it out yourself. All the tips and hints in the world wont help unless you try it out yourself, but they may help you get started in the right direction.

最后,真正了解什么有用,什么没用的唯一方法就是自己去尝试。除非你亲自尝试,否则世界上所有的提示和提示都不会有帮助,但它们可能会帮助你朝正确的方向开始。

#1


38  

Each "application" should be small -- a single reusable entity plus a few associated tables. We have about 5 plus/minus 2 tables per application model. Most of our half-dozen applications are smaller than 5 tables. One has zero tables in the model.

每个“应用程序”都应该很小——一个可重用的实体加上几个相关联的表。每个应用程序模型大约有5个正/负2个表。我们的6个应用程序中的大多数都小于5个表。模型中有0个表。

Each application should be designed to be one reusable concept. In our case, each application is a piece of the overall site; the applications could be removed and replaced separately.

每个应用程序都应该设计为一个可重用的概念。在我们的例子中,每个应用程序都是整个站点的一部分;这些应用程序可以分别删除和替换。

Indeed, that's our strategy. As our requirements expand and mature, we can remove and replace applications independently from each other.

的确,这是我们的策略。随着我们的需求的扩展和成熟,我们可以独立地删除和替换应用程序。

It's okay to have applications depend on each other. However, the dependency has to be limited to the obvious things like "models" and "forms". Also, applications can depend on the names in each other's URL's. Consequently, your named URL's must have a form like "application-view" so the reverse function or the {% url %} tag can find them properly.

让应用程序相互依赖是可以的。但是,依赖关系必须局限于诸如“模型”和“表单”之类的明显的东西。此外,应用程序可以依赖于彼此的URL中的名称。因此,您的命名URL必须具有类似“应用程序视图”的表单,以便反向函数或{% URL %}标记能够正确地找到它们。

Each application should contain it's own batch commands (usually via a formal Command that can be found by the django-admin script.

每个应用程序都应该包含自己的批处理命令(通常是通过django-admin脚本可以找到的正式命令)。

Finally, anything that's more complex than a simple model or form that's shared probably doesn't belong to either application, but needs to be a separate shared library. For example, we use XLRD, but wrap parts of it in our own class so it's more like the built-in csv module. This wrapper for XLRD isn't a proper part of any one application, to it's a separate module, outside the Django applications.

最后,任何比简单的模型或共享的表单更复杂的东西可能不属于任何一个应用程序,但需要是一个单独的共享库。例如,我们使用XLRD,但是在我们自己的类中封装它的部分,所以它更像内置的csv模块。XLRD的包装器不是任何一个应用程序的适当部分,对于它来说,它是Django应用程序之外的一个单独的模块。

#2


10  

I've found it to be helpful to take a look at large open-source Django projects and take note of how that project does it. Django's site has a good list of open-source projects:

我发现,看看大型开源Django项目是有帮助的,并注意到这个项目是如何实现的。Django的站点有一个很好的开源项目列表:

http://code.djangoproject.com/wiki/DjangoResources#Open-SourceDjangoprojects

http://code.djangoproject.com/wiki/DjangoResources Open-SourceDjangoprojects

As does Google (although most of these are smaller add-in template tags and Middleware:

谷歌也一样(尽管其中大部分是较小的插件模板标记和中间件):

http://code.google.com/hosting/search?q=label:django

http://code.google.com/hosting/search?q=label django

Of course, just because one project does it one way does not mean that that way is The Right Way (or The Wrong Way). Some of those projects are more successful than others.

当然,仅仅因为一个项目以一种方式进行,并不意味着这种方式是正确的(或者是错误的)。有些项目比其他项目更成功。

In the end, the only way to really learn what works and doesn't work is to try it out yourself. All the tips and hints in the world wont help unless you try it out yourself, but they may help you get started in the right direction.

最后,真正了解什么有用,什么没用的唯一方法就是自己去尝试。除非你亲自尝试,否则世界上所有的提示和提示都不会有帮助,但它们可能会帮助你朝正确的方向开始。