Django - 我应该在哪里放置计算方法来设计一个合适的和可维护的项目?
Django - Where should I place calculation method to design a proper and maintainable project?
我有一些 类 这样的;
class RawMaterial(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
amount = models.IntegerField()
raw_materials = models.ManyToManyField(RawMaterial, through='MaterialProduct', related_name='products')
class MaterialProduct(models.Model):
raw_material = models.ForeignKey(RawMaterial, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
material_price = models.FloatField()
material_rate = models.FloatField()
我想写一个名为 calculate_total_price
的方法,我的方法将使用 Product 的 amount
和 MaterialProduct的material_price
,material_rate
。
要设计一个proper/beautiful/maintainable项目,我应该在哪里写我的方法?要 models.py
或 views.py
?
提前致谢。
按照胖模型瘦视图的方法,我建议你把那个计算放在models.py
。
它可能看起来像这样:
class MaterialProduct(models.Model):
# attributes
def calculate_total_price(self):
# perform calculation with
# self.product.amount
# self.material_price
# self.material_rate
return result
您也可以从模板 ({{ object.calculate_total_price }}
) 调用此方法以显示总价。
现在,如果您需要多次调用此方法,问题就来了:如果结果没有改变,我们为什么要再次 运行 该方法?
因此我会更进一步,将其设为 属性:
class MaterialProduct(models.Model):
# attributes
@property
def total_price(self):
# perform calculation
return result
或者,如前所述,如果您不希望总价每隔几秒变化一次,也许您想使用 cached_property
:
from django.utils.functional import cached_property
class MaterialProduct(models.Model):
# attributes
@cached_property
def total_price(self):
# perform calculation
return result
总价现在与模板中的任何其他字段一样可用 ({{ object.total_price }}
)。如果您使用 cached_property
,计算将只执行一次,结果将被缓存。再次调用 属性 将从缓存中检索结果,您可以将命中保存到数据库和 CPU 处理时间。
我有一些 类 这样的;
class RawMaterial(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
amount = models.IntegerField()
raw_materials = models.ManyToManyField(RawMaterial, through='MaterialProduct', related_name='products')
class MaterialProduct(models.Model):
raw_material = models.ForeignKey(RawMaterial, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
material_price = models.FloatField()
material_rate = models.FloatField()
我想写一个名为 calculate_total_price
的方法,我的方法将使用 Product 的 amount
和 MaterialProduct的material_price
,material_rate
。
要设计一个proper/beautiful/maintainable项目,我应该在哪里写我的方法?要 models.py
或 views.py
?
提前致谢。
按照胖模型瘦视图的方法,我建议你把那个计算放在models.py
。
它可能看起来像这样:
class MaterialProduct(models.Model):
# attributes
def calculate_total_price(self):
# perform calculation with
# self.product.amount
# self.material_price
# self.material_rate
return result
您也可以从模板 ({{ object.calculate_total_price }}
) 调用此方法以显示总价。
现在,如果您需要多次调用此方法,问题就来了:如果结果没有改变,我们为什么要再次 运行 该方法?
因此我会更进一步,将其设为 属性:
class MaterialProduct(models.Model):
# attributes
@property
def total_price(self):
# perform calculation
return result
或者,如前所述,如果您不希望总价每隔几秒变化一次,也许您想使用 cached_property
:
from django.utils.functional import cached_property
class MaterialProduct(models.Model):
# attributes
@cached_property
def total_price(self):
# perform calculation
return result
总价现在与模板中的任何其他字段一样可用 ({{ object.total_price }}
)。如果您使用 cached_property
,计算将只执行一次,结果将被缓存。再次调用 属性 将从缓存中检索结果,您可以将命中保存到数据库和 CPU 处理时间。