UnboundLocalError: local variable 'xxx' referenced before assignmen

调用一下接口 报错 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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值