调用一下接口 报错 UnboundLocalError: local variable 'id_dict' referenced before assignmen
# 根据性能测试序列号获取任务ID
@action(detail=False, methods=['get'], url_name="根据性能获取任务ID")
def get_task_id(self, request):
message = "查询成功"
success = True
# 作业ID job_id
try:
# serial_id序列号为performance_inst_server表主键
serial_id = request.query_params.get("serial_id")
logging.info("serial_id:%s" % serial_id)
inst_server_obj = PerformanceInstServer.objects.get(id=serial_id)
inst_id = inst_server_obj.instId
inst_detail_obj = PerformanceInstDetail.objects.get(inst_id=inst_id)
uuid = inst_detail_obj.jobId
task_record_obj = TaskRecord.objects.get(uuid=uuid)
id_info = {"id": task_record_obj.id}
except Exception as e:
message = str(e)
success = False
# try 模块报错 然后执行 return 时 id_info就找不到这个变量 没有定义
return getResponse(Response(id_info), message, success)
solution:
在 try 之前 给id_dict 赋予变量一个初始空值 假如 try 里面的代码报错 是找不到 id_dict这个变量的
# 根据性能测试序列号获取任务ID
@action(detail=False, methods=['get'], url_name="根据性能获取任务ID")
def get_task_id(self, request):
message = "查询成功"
success = True
id_info = dict()//没有考虑假如报错 不走try代码块 代码就找不到这个id_info 变量啦
# 作业ID job_id
try:
# serial_id序列号为performance_inst_server表主键
serial_id = request.query_params.get("serial_id")
logging.info("serial_id:%s" % serial_id)
inst_server_obj = PerformanceInstServer.objects.get(id=serial_id)
inst_id = inst_server_obj.instId
inst_detail_obj = PerformanceInstDetail.objects.get(inst_id=inst_id)
uuid = inst_detail_obj.jobId
task_record_obj = TaskRecord.objects.get(uuid=uuid)
id_info = {"id": task_record_obj.id}
except Exception as e:
message = str(e)
success = False
id_info = id_info
return getResponse(Response(id_info), message, success)