首页 > 解决方案 > 序列项 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))

请问有什么帮助吗?

标签: pythonodoo

解决方案


抱歉,回复晚了,我实际上并没有太多关于您要做什么的信息,但这可能是一个解决方案:

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)

推荐阅读