models.py变得巨大,最好的破解方式是什么? [英] models.py getting huge, what is the best way to break it up?

查看:106
本文介绍了models.py变得巨大,最好的破解方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主管的路线:
我想避免在 models.py 中放置任何逻辑,从这里开始,我们将其用作只有类用于访问数据库,并将所有逻辑保留在使用模型类的外部类中,或将其包装起来。



我觉得这是错误的方法。我觉得保持逻辑只是为了保持文件小,是一个坏主意。如果逻辑在模型中是最好的,那么无论文件大小如何,这是真的应该去的地方。



所以有一个简单的方法来使用include?在PHP中,我想向主管提出我们刚刚从其他地方提供模型类的 models.py include()。在概念上,这将允许模型具有我们想要的所有逻辑,但是通过增加文件数量(导致更少的修订控制问题,如冲突等)来保持文件大小。



那么,有没有一种简单的方法来从models.py文件中删除模型类,但仍然可以使用所有的Django工具?或者,对于大models.py文件的一般问题,是否有完全不同但优雅的解决方案?任何输入都将不胜感激。

解决方案

Django旨在让您构建许多小型应用程序,而不是一个大型应用程序。 >

在每个大型应用程序中,许多小型应用程序都在努力免费。



如果您的模型。 py 感觉很大,你做的太多了。停止。放松。分解。



查找较小的,可重复使用的小应用程序组件或块。你不必实际重用它们。请仔细考虑一下它们是否可重复使用。



考虑升级路径并分解您可能希望在某一天进行更换的应用程序。您不需要替换它们,但您可以将它们视为编程的独立模块,可能会被将来更冷的东西所替代。



我们有大约十几个应用程序,每个 model.py 不超过约400行代码。他们都非常专注于不到几十个离散类定义。 (这些不是很严格的限制,它们是关于我们的代码的意见。)



我们早期和经常分解。


Directions from my supervisor: "I want to avoid putting any logic in the models.py. From here on out, let's use that as only classes for accessing the database, and keep all logic in external classes that use the models classes, or wrap them."

I feel like this is the wrong way to go. I feel that keeping logic out of the models just to keep the file small is a bad idea. If the logic is best in the model, that's where it really should go regardless of file size.

So is there a simple way to just use includes? In PHP-speak, I'd like to propose to the supervisor that we just have models.py include() the model classes from other places. Conceptually, this would allow the models to have all the logic we want, yet keep file size down via increasing the number of files (which leads to less revision control problems like conflicts, etc.).

So, is there a simple way to remove model classes from the models.py file, but still have the models work with all of the Django tools? Or, is there a completely different yet elegant solution to the general problem of a "large" models.py file? Any input would be appreciated.

解决方案

Django is designed to let you build many small applications instead of one big application.

Inside every large application are many small applications struggling to be free.

If your models.py feels big, you're doing too much. Stop. Relax. Decompose.

Find smaller, potentially reusable small application components, or pieces. You don't have to actually reuse them. Just think about them as potentially reusable.

Consider your upgrade paths and decompose applications that you might want to replace some day. You don't have to actually replace them, but you can consider them as a stand-alone "module" of programming that might get replaced with something cooler in the future.

We have about a dozen applications, each model.py is no more than about 400 lines of code. They're all pretty focused on less than about half-dozen discrete class definitions. (These aren't hard limits, they're observations about our code.)

We decompose early and often.

这篇关于models.py变得巨大,最好的破解方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆