models.py 变得很大,分解它的最佳方法是什么? [英] models.py getting huge, what is the best way to break it up?

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

问题描述

来自我的主管的指示:我想避免在 models.py 中放置任何逻辑.从现在开始,让我们仅将其用作访问数据库的类,并将所有逻辑保留在使用模型类的外部类中,或包裹它们."

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.

那么有没有一种简单的方法来使用包含?用 PHP 的话说,我想向主管建议我们只有 models.py include() 来自其他地方的模型类.从概念上讲,这将允许模型具有我们想要的所有逻辑,同时通过增加文件数量来减小文件大小(这会减少诸如冲突等修订控制问题).

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.).

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

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 旨在让您构建许多小应用程序,而不是一个大应用程序.

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.

如果你的 models.py 感觉很大,那你做的太多了.停止.放松.分解.

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.

我们有大约十几个应用程序,每个model.py 不超过大约 400 行代码.他们都非常专注于不到六个离散类定义.(这些不是硬性限制,它们是对我们代码的观察.)

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天全站免登陆