Django 模型 - 在 class 中混合模型字段和实例属性
Django Model - Mix model fields and instance attributes in class
不确定我是否完全走错了路,但我想在我的 Django 模型中使用某种临时运行时 variable/field。目前我有这个:
class BioChemicalProperties(models.Model):
mw = models.FloatField(blank=True, null=True)
iep = models.FloatField(blank=True, null=True)
exco = models.FloatField(blank=True, null=True)
molar_exco = models.FloatField(blank=True, null=True)
e01 = models.FloatField(blank=True, null=True)
def get_protein(self):
aa_seq = self.ab.get_lc_protein()
aa_seq += self.ab.get_hc_protein()
return aa_seq
def calc_mw(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(aa_seq)
self.mw = analysed_seq.molecular_weight() + 18.015
def calc_exco(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(aa_seq)
self.exco = analysed_seq.molar_extinction_coefficient()[1]
#...more methods here...
def calc_all_bioprops(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
self.calc_mw(aa_seq)
self.calc_iep(aa_seq)
self.calc_molexco(aa_seq)
self.calc_e01(aa_seq)
aa_seq
变量是不应存储在数据库中的临时值。然而,为了在使用多个方法时不重新计算每个方法的值,我想有选择地将它作为参数提供。我看到在每个方法中将 aa_seq
作为参数是多余的,而且也不是面向对象的。现在我想知道将它存储为 class 属性 是否是一个好主意(并且可能),例如:
class BioChemicalProperties(models.Model):
mw = models.FloatField(blank=True, null=True)
iep = models.FloatField(blank=True, null=True)
exco = models.FloatField(blank=True, null=True)
molar_exco = models.FloatField(blank=True, null=True)
e01 = models.FloatField(blank=True, null=True)
aa_seq = ""
def calc_mw(self):
if not self.aa_seq:
self.aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(self.aa_seq)
self.mw = analysed_seq.molecular_weight() + 18.015
但是,我还没有找到模型字段和非模型字段混合的任何类似示例...这有什么原因吗?
看起来 cached_property
可以做的事情:
from django.utils.functional import cached_property
class BioChemicalProperties(models.Model):
...
@cached_property
def aa_seq(self):
return self.ab.get_lc_protein() + self.ab.get_hc_protein()
然后您可以像现在一样使用 self.aa_seq
,并且可以删除 if not aa_seq
检查,因为它不再需要了。此外,这将在实例的生命周期内缓存 aa_seq
的计算值,而不会将任何内容存储到数据库中。
你的想法很好,但是你的术语很混乱。
将其存储为实例属性绝对没有错。这不是一个领域,它在模型上是巧合。模型只是 类.
的特例
Here 是在 Django 文档本身的 Model
子类上定义非字段属性的示例。
不确定我是否完全走错了路,但我想在我的 Django 模型中使用某种临时运行时 variable/field。目前我有这个:
class BioChemicalProperties(models.Model):
mw = models.FloatField(blank=True, null=True)
iep = models.FloatField(blank=True, null=True)
exco = models.FloatField(blank=True, null=True)
molar_exco = models.FloatField(blank=True, null=True)
e01 = models.FloatField(blank=True, null=True)
def get_protein(self):
aa_seq = self.ab.get_lc_protein()
aa_seq += self.ab.get_hc_protein()
return aa_seq
def calc_mw(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(aa_seq)
self.mw = analysed_seq.molecular_weight() + 18.015
def calc_exco(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(aa_seq)
self.exco = analysed_seq.molar_extinction_coefficient()[1]
#...more methods here...
def calc_all_bioprops(self, aa_seq=None):
if not aa_seq:
aa_seq = self.get_protein()
self.calc_mw(aa_seq)
self.calc_iep(aa_seq)
self.calc_molexco(aa_seq)
self.calc_e01(aa_seq)
aa_seq
变量是不应存储在数据库中的临时值。然而,为了在使用多个方法时不重新计算每个方法的值,我想有选择地将它作为参数提供。我看到在每个方法中将 aa_seq
作为参数是多余的,而且也不是面向对象的。现在我想知道将它存储为 class 属性 是否是一个好主意(并且可能),例如:
class BioChemicalProperties(models.Model):
mw = models.FloatField(blank=True, null=True)
iep = models.FloatField(blank=True, null=True)
exco = models.FloatField(blank=True, null=True)
molar_exco = models.FloatField(blank=True, null=True)
e01 = models.FloatField(blank=True, null=True)
aa_seq = ""
def calc_mw(self):
if not self.aa_seq:
self.aa_seq = self.get_protein()
analysed_seq = ProteinAnalysis(self.aa_seq)
self.mw = analysed_seq.molecular_weight() + 18.015
但是,我还没有找到模型字段和非模型字段混合的任何类似示例...这有什么原因吗?
看起来 cached_property
可以做的事情:
from django.utils.functional import cached_property
class BioChemicalProperties(models.Model):
...
@cached_property
def aa_seq(self):
return self.ab.get_lc_protein() + self.ab.get_hc_protein()
然后您可以像现在一样使用 self.aa_seq
,并且可以删除 if not aa_seq
检查,因为它不再需要了。此外,这将在实例的生命周期内缓存 aa_seq
的计算值,而不会将任何内容存储到数据库中。
你的想法很好,但是你的术语很混乱。
将其存储为实例属性绝对没有错。这不是一个领域,它在模型上是巧合。模型只是 类.
的特例Here 是在 Django 文档本身的 Model
子类上定义非字段属性的示例。