python - 序列项 0:预期的 str 实例,找到 bool (Odoo 14)
问题描述
我收到错误“在 _get_report_values map(lambda x: x.ref, docs.account_invoice_ids)) 或 ', '.join( TypeError: sequence item 0: expected str instance, bool found" 中。
data['test'] = docs.type == 'type1' and ', '.join(
map(lambda x: x.reference, docs.acc)) or ', '.join(
map(lambda x: x.name, docs.acc))
请问有什么帮助吗?
解决方案
抱歉,回复晚了,我实际上并没有太多关于您要做什么的信息,但这可能是一个解决方案:
data['test'] = ', '.join(map(lambda x: x.reference, docs.acc)) if docs.type == 'type1' else ', '.join(map(lambda x: x.name, docs.acc))
x.reference
如果文档类型是type1
其他类型,则使用此语法加入x.name
编辑: 正如@sabik 指出的那样,您还可以使用生成器而不是 map 函数来简化语句。如果整个集合的文档类型相同,则可以通过以下方式简化语句:
data['test'] = ', '.join(x.reference for x in docs.acc) if docs.type == 'type1' else ', '.join(x.name for x in docs.acc)
此外,为了便于阅读,您还可以尝试以下操作:
doc_attr = "reference" if docs.type == "type1" else "name"
data["test"] = ", ".join(getattr(x, doc_attr) for x in docs.acc)
否则,如果每个“x”的类型发生变化,您可以使用
data['test'] = ', '.join(x.reference if x.type == 'type1' else x.name for x in docs.acc)
推荐阅读
- c# - 我的 wcf 客户端显示异常,但 WcfTestClient 工具工作正常
- javascript - 无法在 IE11/Windows 上预览 PDF 文件
- javascript - 如何仅使用 JavaScript 通过单击按钮将用户重定向到另一个页面?
- ruby-on-rails-4 - Crud 操作“删除”在我的第一个 ruby on rails 项目中不起作用
- google-cloud-platform - 如何检查您的 GoogleColab 运行的区域?
- javascript - 在用户关闭模式后加载函数
- python - SQLAlchemy before_execute 和绑定参数
- node.js - 文本搜索返回空数组
- sql-server - 带有 AG 侦听器的 SSRS 中的数据源连接字符串
- angular - 在 nativescript(typescript + angular)中不使用 formdata 发布图像和其他信息