简体   繁体   English

我是否正确组织了我的django应用程序?

[英]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.

相关问题 我的 Django 帐户应用程序出现错误 - I have an error in my Django accounts app 我无法为 Heroku 配置我的 Django 应用程序 - I am have trouble configuring my Django app for Heroku Django:每次我开发 django 应用程序时都必须打开虚拟环境吗 - Django: Does the virtual environment have to be on every time i develop my django app 如何正确使用此Django应用程序? - How do I use this Django app correctly? 如何将Heroku的数据库存储到本地计算机? 我有一个Django应用(不是Rails) - How do I database off Heroku to my local machine? I have a Django app (not Rails) 我必须使用 REST api 将我公司的应用程序与 Jira 集成并使用基本身份验证。我必须在 django 结构中进行 - I have to integrate my company's app with Jira using REST api and use the basic authentication.. I have to do it in django structure 我刚刚在线上传了我的Django应用程序,它可以运行,但是找不到其他应用程序 - I have just uploaded my Django app online, it works, however other apps are not being found 我有一个在django应用程序上本地运行的功能,但部署该功能会导致列表索引超出范围错误 - I have afunction that works locally on my django app but deployed it raises an list index out of range error Django 无法识别我的 Model 中有对象 - Django not recognizing I have objects in my Model 我的 python 代码有问题无法正确执行 - I have a problem in my python code to not execute correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM