我有这个代码:
def get_percentage_completions(task: Task, date_start: date, date_end: date):
result = {}
execution_percents = task.execution_percents.all()
if execution_percents.exists():
year, week, _ = date_start.isocalendar()
task_execution_percent = execution_percents.filter(year__lte=year, week__lte=week) \
.order_by('year', 'week').last()
execution_percent = task_execution_percent.percent if task_execution_percent else 0
while date_end >= date_start:
year, week, _ = date_start.isocalendar()
if execution_percents.filter(year=year, week=week).exists():
task_execution_percent = execution_percents.filter(year=year, week=week).first()
execution_percent = task_execution_percent.percent
key = f"{year}_{week}"
result[key] = round(execution_percent)
date_start += timedelta(days=7)
return result
我从我的模型“任务”中获取相关对象。 当我定义 execution_persents 变量时,我认为我在第二行点击了一次 db, 但实际上,当我在 execution_persents 变量后注释所有行时,我的调试工具栏减少了查询量。 这意味着我在循环中点击了 db。
我可以在第二行只点击一次数据库吗?