相同的查询集求值代码,不同的字段名在Django视图



我有看似冗余的代码在Django视图评估相同的查询集(Model.objects.all()),但有不同的字段名称:

def overview_view(request):
bacteria = Bacteria.objects.all()
bacteria_count = bacteria.count()
bacteriaFilter = BacteriaFilter(request.GET, queryset=bacteria)
bacteria = bacteriaFilter.qs
remaining = bacteria.count()
glucose = []
not_glucose = []
pure_glucose = []
for bug in bacteria:
if (bug.glucose_acid_from is not None) and (bug.glucose_acid_from != 'neg'):
glucose.append(bug.species)
for bug in bacteria:
if (bug.glucose_use is not None) and (bug.glucose_use != 'neg'):
glucose.append(bug.species)
any_glucose = len(set(glucose))
for bug in bacteria:
if (bug.glucose_use is not None) and (bug.glucose_use == 'neg') and (bug.glucose_acid_from is not None) and (bug.glucose_acid_from == 'neg'):
not_glucose.append(bug.species)
no_glucose = len(set(not_glucose))
for bug in bacteria:
if (bug.glucose_use is not None) and (bug.glucose_use == '+'):
pure_glucose.append(bug.species)
for bug in bacteria:
if (bug.glucose_acid_from is not None) and (bug.glucose_acid_from == '+'):
pure_glucose.append(bug.species)
mix_glucose = any_glucose - len(set(pure_glucose))
nonr_glucose = bacteria_count - any_glucose
fructose = []
not_fructose = []
pure_fructose = []
for bug in bacteria:
if (bug.fructose_acid_from is not None) and (bug.fructose_acid_from != 'neg'):
fructose.append(bug.species)
for bug in bacteria:
if (bug.fructose_use is not None) and (bug.fructose_use != 'neg'):
fructose.append(bug.species)
any_fructose = len(set(fructose))
for bug in bacteria:
if (bug.fructose_use is not None) and (bug.fructose_use == 'neg') and (bug.fructose_acid_from is not None) and (bug.fructose_acid_from == 'neg'):
not_fructose.append(bug.species)
no_fructose = len(set(not_fructose))
for bug in bacteria:
if (bug.fructose_use is not None) and (bug.fructose_use == '+'):
pure_fructose.append(bug.species)
for bug in bacteria:
if (bug.fructose_acid_from is not None) and (bug.fructose_acid_from == '+'):
pure_fructose.append(bug.species)
mix_fructose = any_fructose - len(set(pure_fructose))
nonr_fructose = bacteria_count - any_fructose ...etc.

我使用的数据从这个视图填充表在一个html页面:

<div>
<hr />
<table class="table table-sortable table-bordered table-hover">
<thead>
<p>Bacteria utilisation or oxidation/fermentation (O/F) of selected carbohydrates</p>
<tr>
<th>total</th>
<th>Carbohydrate</th>
<th>Any use or O/F</th>
<th>Mixed response (w, d, vr)</th>
<th>Neg for both</th>
<th>Neg or not reported</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{bacteria_count}}</td>
<td>glucose</td>
<td>{{any_glucose}}</td>
<td>{{mix_glucose}}</td>
<td>{{no_glucose}}</td>
<td>{{nonr_glucose}}</td>
</tr>
<tr>
<td>{{bacteria_count}}</td>
<td>fructose</td>
<td>{{any_fructose}}</td>
<td>{{mix_fructose}}</td>
<td>{{no_fructose}}</td>
<td>{{nonr_fructose}}</td>
</tr> ... etc.

给出所需的表:

输入图片描述

但是,我想问:

  1. 对来自远程数据库的数据进行多次评估是否有很大的成本?我的理解是,bacteria = Bacteria.objects.all()查询集缓存在内存中,可以由后续查询重新计算。这适用于这里吗?
  2. 代码非常重复,唯一改变的是字段名。有没有更好的方法来组织这些代码?
  3. 最后,我希望允许自定义用户输入不同的字段名,而不是硬编码它们。是否有可能在飞行中替换点符号字段?谢谢你。

在这种情况下,过滤现有的查询集确实会对数据库进行额外的调用,因为第一个查询集已经通过.count()进行了计算。但是,count()比完整查询更有效,因此开销不是很大。除非你想提高效率,否则这是可以的。

然而,当您使用相同的循环项并且经常测试相同的东西时,多次循环遍历记录集有点低效。下面的语句只循环一次,并将一些If语句分组,这样更容易看到模式:

def overview_view(请求):细菌=细菌。物体。所有()Bacteria_count = bacteria.count()

bacteriaFilter = BacteriaFilter(request.GET, queryset=bacteria)
bacteria = bacteriaFilter.qs
remaining = bacteria.count()
glucose = []
not_glucose = []
pure_glucose = []
fructose = []
not_fructose = []
pure_fructose = []
for bug in bacteria:
if (bug.glucose_acid_from is not None):
if (bug.glucose_acid_from != 'neg'):
glucose.append(bug.species)
if (bug.glucose_use is not None) and (bug.glucose_use == 'neg')  and (bug.glucose_acid_from == 'neg'):
not_glucose.append(bug.species)            
if (bug.glucose_acid_from == '+'):
pure_glucose.append(bug.species)

if (bug.glucose_use is not None): 
if (bug.glucose_use != 'neg'):
glucose.append(bug.species)

if (bug.glucose_use == '+'):
pure_glucose.append(bug.species)


if (bug.fructose_acid_from is not None): 
if (bug.fructose_acid_from != 'neg'):
fructose.append(bug.species)

if (bug.fructose_use is not None) and (bug.fructose_use == 'neg') and (bug.fructose_acid_from == 'neg'):
not_fructose.append(bug.species)

if (bug.fructose_acid_from == '+'):
pure_fructose.append(bug.species)               
if (bug.fructose_use is not None):
if (bug.fructose_use != 'neg'):
fructose.append(bug.species)     

if  (bug.fructose_use == '+'):
pure_fructose.append(bug.species)


any_glucose = len(set(glucose))        
no_glucose = len(set(not_glucose))
mix_glucose = any_glucose - len(set(pure_glucose))
nonr_glucose = bacteria_count - any_glucose        
any_fructose = len(set(fructose))  
no_fructose = len(set(not_fructose))      
mix_fructose = any_fructose - len(set(pure_fructose))
nonr_fructose = bacteria_count - any_fructose ...etc.

对于动态字段名,可以尝试:

str_variable = "glucose_acid_from"
bacteria = Bacteria.objects.all.values()
for bug in bacteria:
if bug[str_variable] is not None:

最新更新