数据库读取:AttributeError: ‘list‘ object has no attribute ‘id‘

在多表连接查询时,出现下列问题:

[E 200702 18:42:49 web:1524] Uncaught exception POST /accountant/order (127.0.0.1)
    Traceback (most recent call last):
      File "/usr/local/lib/python3.7/site-packages/tornado/web.py", line 1443, in _execute
        result = method(*self.path_args, **self.path_kwargs)
      File "/Users/morika/Desktop/pf.senguo.cc/pfsource/libs/webbase.py", line 95, in wrapper
        return method(self, *args, **kwargs)
      File "/Users/morika/Desktop/pf.senguo.cc/pfsource/handlers/accountant.py", line 3456, in post
        return self.get_order_list(shop_id)
      File "/Users/morika/Desktop/pf.senguo.cc/pfsource/libs/webbase.py", line 95, in wrapper
        return method(self, *args, **kwargs)
      File "/Users/morika/Desktop/pf.senguo.cc/pfsource/handlers/boss_accountant.py", line 321, in get_order_list
        print(account_info.id)
    AttributeError: 'list' object has no attribute 'id'


原始代码如下:

if goods_id_list:
            account_info = session.query(Accountinfo). \
                join(GoodsSalesRecord, GoodsSalesRecord.salesman_id == Accountinfo.id) \
                .filter(GoodsSalesRecord.goods_id.in_(goods_id_list)) \
                .all()
            print(account_info)
            salesman_info["salesman_id"] = account_info.id
            salesman_info["salesman_name"] = account_info.nickname
            salesman_info["salesman_sex"] = account_info.sex
            salesman_info["salesman_wximgurl"] = account_info.headimgurl

打印出结果:

[<dal.models.Accountinfo object at 0x12009aa90>, <dal.models.Accountinfo object at 0x12009ab10>]

但同样报错,提示AttributeError: ‘list’ object has no attribute ‘id’

原因是查询时使用all()方法,即查询所有符合条件的对象,在打印结果之后读取对象的元素。

从打印结果来看,是一个list,里面有两个acctountinfo 对象,即
salesman_info = [model1, model2]

所以在后面读取model对象的元素时,没有获取到

解决方法是对列表切片读取或者查询时至获取符合条件的第一个对象,即查询时使用first() 而不是all()

if goods_id_list:
            account_info = session.query(Accountinfo). \
                join(GoodsSalesRecord, GoodsSalesRecord.salesman_id == Accountinfo.id) \
                .filter(GoodsSalesRecord.goods_id.in_(goods_id_list)) \
                .all()
            salesman_info["salesman_id"] = account_info.id
            salesman_info["salesman_name"] = account_info.nickname
            salesman_info["salesman_sex"] = account_info.sex
            salesman_info["salesman_wximgurl"] = account_info.headimgurl

问题即可解决。

这个错误是因为在代码中使用了一个列表对象,而列表对象没有"add_"属性。根据引用\[1\]中的信息,这个错误可能是由于在代码中使用了一个列表对象的split()方法,而列表对象没有split()方法。要解决这个问题,你需要检查代码中的ls=tf.readlines()这一行,确保你正在读取的是一个文件而不是一个列表对象。如果你确实想要对列表对象进行split操作,你可以使用其他方法来实现,比如使用字符串的join()方法将列表中的元素连接起来,然后再使用split()方法进行分割。 #### 引用[.reference_title] - *1* [AttributeError: ‘listobject has no attribute ‘split‘ 报错](https://blog.csdn.net/MaoNanBei2233/article/details/110046587)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* *3* [AttributeError: ‘listobject has no attribute ‘size‘解决办法](https://blog.csdn.net/qq_38703529/article/details/122738347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值