繁体   English   中英

如何从 orm 中获取数据库值和从 odoo 8 中的字符串获取字段名

[英]how to get database value from orm and field name get from string in odoo 8

我从配置中获取字段名称字符串。

para = 'partner_id.name'
account.invoice(281088,).para 
*** AttributeError: 'account.invoice' object has no attribute 'para'

系统无法准确知道数据库字段名称。它来自用户配置。

在您的情况下,您可以拆分提供的参数并像这样使用getattr

para = 'partner_id.name'
attributes = para.split('.')
value = account.invoice(281088)
for attribute in attributes:
    if hasattr(value, attribute):
        value = getattr(value, attribute)
    else:
        return False
return value
attributes = para.split('.')

这将拆分用户提供的参数。 在这种情况下,属性值将变为['partner_id', 'name']

value = account.invoice(281088)

将 account.invoice 分配给值

for attribute in attributes:
    if hasattr(value, attribute):
        value = getattr(value, attribute)
    else:
        return False

循环遍历我们的属性并检查 value 是否具有该属性。 如果是,则获取其值或以其他方式返回。

return value

在这种情况下返回将是合作伙伴名称的结局值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM