diving deeper into django I came across the challenge to handle code which is not specific to 1 app but is shared/used by multiple apps.
深入研究django,我遇到了处理代码的挑战,这些代码不是特定于一个应用程序,而是由多个应用程序共享/使用的。
I would not(!) like to store it as part of an app (to avoid app dependencies) but to have it in a specific place.
我不想把它作为应用程序的一部分来存储(为了避免应用程序依赖),而是把它放在一个特定的地方。
currently my best practice is to create a django app "shared" in which I place this code/functionality
目前,我的最佳实践是创建一个django应用程序“shared”,在其中放置代码/功能
so my project structure looks similar to this:
所以我的项目结构和这个类似:
mysite/
manage.py
mysite/
...
shared
...
app1
...
app2
...
app3
...
...
is there a "django best parctice" or a more practical way how to handle this?
有“django最好的分布”吗?
1 个解决方案
#1
1
I usually do exact same thing what you are doing. Not sure if that is best practice however I've seen other people using the same approach. I like it because:
我通常和你做同样的事情。我不确定这是否是最佳实践,但我看到其他人也在使用同样的方法。我喜欢它,因为:
- when the
shared
/core
/etc app becomes useful in other projects, you can package it as reusable app which can be installed viapip
in other projects - 当共享/核心/等应用程序在其他项目中变得有用时,您可以将其打包为可重用应用程序,可以通过pip在其他项目中安装。
- it does not interfere with existing apps in the project
- 它不会干扰项目中的现有应用程序
The only note about packaging it as a reusable lib is that I would recommend to rename it to something other then shared
. The reasons is that when you package it in PyPI lets say as my_django_utils
, then you will have to change all your imports in all the projects. If you come up with a generic name now, then you can easily package it in the future without needing to change all your imports...
将它打包为可重用库的惟一注意事项是,我建议将它重命名为其他可共享的东西。原因是,当您在PyPI中包它时,假设它是my_django_utils,那么您将不得不更改所有项目中的所有导入。如果您现在有一个通用的名称,那么您可以在将来轻松地打包它,而不需要更改所有的导入……
#1
1
I usually do exact same thing what you are doing. Not sure if that is best practice however I've seen other people using the same approach. I like it because:
我通常和你做同样的事情。我不确定这是否是最佳实践,但我看到其他人也在使用同样的方法。我喜欢它,因为:
- when the
shared
/core
/etc app becomes useful in other projects, you can package it as reusable app which can be installed viapip
in other projects - 当共享/核心/等应用程序在其他项目中变得有用时,您可以将其打包为可重用应用程序,可以通过pip在其他项目中安装。
- it does not interfere with existing apps in the project
- 它不会干扰项目中的现有应用程序
The only note about packaging it as a reusable lib is that I would recommend to rename it to something other then shared
. The reasons is that when you package it in PyPI lets say as my_django_utils
, then you will have to change all your imports in all the projects. If you come up with a generic name now, then you can easily package it in the future without needing to change all your imports...
将它打包为可重用库的惟一注意事项是,我建议将它重命名为其他可共享的东西。原因是,当您在PyPI中包它时,假设它是my_django_utils,那么您将不得不更改所有项目中的所有导入。如果您现在有一个通用的名称,那么您可以在将来轻松地打包它,而不需要更改所有的导入……