我有一个相对简单的设置,字符串为Parent
->Child
关系
**Parent Child**
Site BU
BU CT
CT Line
Line WS
WS Assess
所以每个孩子都有models.ForeignKey(Parent)
业务逻辑的结构类似于金字塔
行的级别(1-2-3)取决于它的所有子级WS级别
CT的电平取决于其所有线路的电平
BU的级别与CTs级别相同 站点位于公共汽车层
例如:
WS1 \
WS2 - line 1 -- CT 1 -\
WS3 / / \
line 2 -/ \
CT 2 -- BU 1 -\
.. Site 1
.. CT 3 -- BU 2 -/
..
line 9 -- CT 4 -/
line 10 -/
问题是:
每个级别都有一个属性来设置颜色。 询问站点颜色(金字塔顶部)会在我的开发数据库上启动1266个查询,并使用最少的虚拟数据量。这是一个巨大的数额
有人知道如何更好地建模颜色属性吗
在只有几个站点的生产服务器上,需要4秒以上的时间才能获得站点颜色,目的是添加更多站点
model.py excerpt:
class CT(models.Model):
name = models.CharField(max_length=255, blank=False)
BU = models.ForeignKey(BU, null=True, blank=True, on_delete=models.SET_NULL)
def __str__(self):
return "{}".format(self.name)
def _color(self):
workstations = self.line_set.all()
num_of_workstations = len(workstations)
high_count = 0
for workstation in workstations:
if workstation.color == "high":
high_count = high_count + 1
elif workstation.color == "No Assessments completed":
num_of_workstations = num_of_workstations - 1
if num_of_workstations <=0:
return "No Assessments completed"
else:
if high_count/num_of_workstations > 0.1:
return "high"
elif high_count > 0:
return "medium"
else:
return "low"
color = property(_color)
class Line(models.Model):
ct= models.ForeignKey(CT, null=True, blank=True, on_delete=models.SET_NULL)
name = models.CharField(max_length=255, blank=False)
def __str__(self):
return "{}".format(self.name)
def _color(self):
wts = self.wt_set.all()
num_of_wts = len(wts)
high_count = 0
for wt in wts:
if wt.color == "high":
high_count = high_count + 1
elif wt.color == "No Assessments completed":
num_of_wts = num_of_wts - 1
if num_of_wts <=0:
return "No Assessments completed"
else:
if high_count/num_of_wts > 0.1:
return "high"
elif high_count > 0:
return "medium"
else:
return "low"
color = property(_color)
class WS(models.Model):
line = models.ForeignKey(Line, null=True, blank=True, on_delete=models.SET_NULL)
def _color(self):
try:
latest_assessment = self.assessment_set.latest()
return latest_assessment.color
except:
return "No Assessments completed"
color=property(_color)
目前没有回答
相关问题 更多 >
编程相关推荐