python - 为什么我收到索引错误异常错误?
问题描述
try:
# recheck this bank detail lookup, can optimize this
# Known fact: as getting bank details and making refund is not in sync, we have bank_details against
# one of the shipment id, so not sure of refund will go against specific bank details.
# Current policy: taking latest one
user_obj = self.session.query(User).filter_by(id=self.order_obj.user_id).one()
current_shipment_id = self.shipment_id
current_shipment_obj = self.session.query(Shipment).filter_by(id=current_shipment_id).one()
prev_shipment_id = current_shipment_obj.previous_shipment_id
bank_account_details = self.session.query(BankAccount).filter(
BankAccount.affiliate_order_id==self.order_obj.affiliate_order_id,
BankAccount.user_id==user_obj.mongo_user_id,
BankAccount.shipment_id.in_([current_shipment_id, prev_shipment_id])
).order_by(desc('created_ts')).all()[0] # IndexError
except IndexError:
logger.exception("Unable to process refund, as no bank details found")
{bag_id:51156,current_awb_no:'null',current_shipment_id:'16266090343871907281J',forward_awb_no:'null',forward_shipment_id:'null',invoice_no:'null',jm_order_id:'16260961159105232A',payment_id 异常索引:5118 索引:超出范围
解决方案
因为这个说法
self.session.query(BankAccount).filter(
BankAccount.affiliate_order_id==self.order_obj.affiliate_order_id,
BankAccount.user_id==user_obj.mongo_user_id,
BankAccount.shipment_id.in_([current_shipment_id, prev_shipment_id])
).order_by(desc('created_ts')).all()
长度小于 1,并且不可能从空列表中获取第一个元素。
推荐阅读
- ruby-on-rails - 我应该把神社 image_uploader 文件放在哪里?
- javascript - 使用点击图片上的 URL 更新现有页面
- python - 熊猫读取带有部分标头的数据帧
- regex - 如何从我的输出值中删除双引号,以便可以在另一个 api 请求中传递它?
- java - 需要帮助来简化解决方案
- r - 如何在数据框中的逗号后在单元格中添加换行符
- java - 在java中显示带有模式的打印整数
- wamp - 将备份 WAMP 文件夹镜像到外部硬盘驱动器
- ponyorm - 带有带有自定义哈希的 mixin 的 PonyORM
- python - 解决 Python 内核在 Python 中被杀死的问题