我们有django项目,我们发现有些模型变得巨大。在
class BigModel(models.Model):
"""
Large set of fields
"""
field1 = models.IntegerField()
field2 = models.IntegerField()
field3 = models.IntegerField()
...
fieldN = models.IntegerField()
"""
Large set of methods
"""
def method1(self): pass
def method2(self): pass
def method3(self): pass
...
def methodN(self): pass
我想用方法列表将BigModel
类分成更小的类。但在整个项目中,我们引用了BigModel
类。在
所以我的想法是小步走:
BigModel
类划分为BigFields
和{BigMethods
来自BigFields
。从BigMethods
继承BigModel
。在BigModel
的引用,减少{因此,在重构代码时,我们的代码如下所示:
^{pr2}$
如果您的模型中有这样的顺序字段,那么解决方案不是继承,而是将这些字段拆分为单独的模型并创建一对多关系。很难用您的示例模型来说明这一点,所以我将使用我正在从事的一个项目中的一个。在
最初的模型看起来像这样:
显然,这是一个需要维护的噩梦,因此我将其更改为以下内容:
^{pr2}$相关问题 更多 >
编程相关推荐