[英]Have I organised my django app correctly?
I'm in a situation where I need to merge two Django apps into a single, re-usable app. 我需要将两个Django应用程序合并到一个可重复使用的应用程序中。 Neither are particularly large, but they are certainly not trivial apps and to preserve readability / sanity I'm trying to keep the two apps separated to some extent. 两者都不是特别大,但它们肯定不是简单的应用程序,为了保持可读性/理智性,我试图保持两个应用程序在某种程度上分离。
I could set up each app as a sub-package (which would be a pythonic way to achieve this end), or I can stick to Django's conventions and split the functionality separately in each case. 我可以将每个应用程序设置为子包(这将是实现此目的的pythonic方式),或者我可以坚持Django的约定并在每种情况下单独拆分功能。
A Pythonic 'sub-package' approach: Pythonic'子包'方法:
package
|-- __init__.py
|-- views.py
|-- models.py # imports models from both sub-packages
|-- tests.py # imports TestCase instances from both sub-packages
|-- etc. # shared stuff
|-- a
| |-- __init__.py
| |-- views.py
| |-- models.py
| |-- tests.py
| `-- etc. # a-specific code
`-- b
|-- __init__.py
|-- views.py
|-- models.py
|-- tests.py
`-- etc. # b-specific code
Or to appease the Django gods more directly: 或者更直接地安抚Django众神:
package
|-- __init__.py
|-- views
| |-- __init__.py
| |-- a.py
| `-- b.py
|-- models
| |-- __init__.py # imports models from a and b
| |-- a.py
| `-- b.py
|-- tests
| |-- __init__.py # imports TestCase instances from a and b
| |-- a.py
| `-- b.py
`-- etc. # shared/additional files
While I'd lean towards the former at the moment (which feels a little lighter), my gut tells me that although both work (and both involve importing 'hacks' to conform to Django's structure) the best choice depends on the contents of a and b - specifically how much of the code is shared or app-specific. 虽然我现在倾向于前者(感觉有点轻),但我的直觉告诉我,虽然两者都有效(并且都涉及导入'hacks'以符合Django的结构),但最好的选择取决于a的内容。和b - 具体是代码共享多少或特定于应用程序。 It doesn't feel right to be constantly repeating the __ init__.py, a.py, b.py pattern in every subdirectory! 在每个子目录中不断重复__ init__.py,a.py,b.py模式是不对的!
I'm interested in knowing which would is more appropriate from people with more experience dealing with Python! 我有兴趣知道哪些人更适合处理Python的人!
ps. PS。 I'm aware they could live as two distinct apps, but they are so inter-dependant now that I do feel like they should be merged! 我知道他们可以作为两个不同的应用程序生活,但他们是如此相互依赖,我觉得他们应该合并! (even aside from the improved portability of a single Django app) (甚至除了单个Django应用程序的改进的可移植性之外)
Flat is better than nested. Flat优于嵌套。
A "composite" application, built from two peer applications is fine. 从两个对等应用程序构建的“复合”应用程序很好。 It works well. 它运作良好。
And it promotes reuse by allowing the two components to be "plug-and-play" options in the larger application. 它通过允许两个组件在更大的应用程序中成为“即插即用”选项来促进重用。
Don't nest things unless you're forced to. 除非你被迫,否则不要筑巢。 The number one reason forcing you to nest is name collisions. 强迫你嵌套的首要原因是名称冲突。 You don't have that so you don't really need any nesting. 你没有这个,所以你真的不需要任何嵌套。
I'm not an expert in python but I always like to seperate applications and other artifact out as much as I can. 我不是python的专家,但我总是喜欢尽可能多地分离应用程序和其他工件。
I am following the approach that is described in this blog for my own django project and it requires a little bit of tweaking on django. 我正在遵循本博客中针对我自己的django项目所描述的方法,它需要对django进行一些调整。 It has served me well so far. 到目前为止,它对我很有帮助。
The direct link to the github project 直接链接到github项目
In my projects I often want to organize views and tests somehow, so I use structure like this: 在我的项目中,我经常想以某种方式组织视图和测试,所以我使用这样的结构:
package
|-- __init__.py
|-- models.py # models are in one file
|-- etc. # shared stuff
|-- tests
| |-- __init__.py
| |-- tests_orders.py
| |-- tests_clients.py
| |--
| `-- etc.
|-- views
| |-- __init__.py
| |-- orders.py
| |-- clients.py
| |--
| `-- etc.
For bigger projects having view functions in one file is a pain (for me). 对于在一个文件中具有视图功能的较大项目来说,这很痛苦(对我而言)。
This is what works for some projects I am working on - hopefully someone find this useful also. 这适用于我正在进行的一些项目 - 希望有人发现这也很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.