Search code examples
pythonodooodoo-8

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


I get field name string from configuration.

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

The system cannot know database field name exactly.It comes from user configuration.


Solution

  • In your case you can split the provided parameter and use getattr like this

    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('.')
    

    This splits user supplied parameter. In this case attributes value will become ['partner_id', 'name']

    value = account.invoice(281088)
    

    Assign account.invoice to value

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

    Loop through our attributes and check if value has that attribute. If so, get its value or return else otherwise.

    return value
    

    Return the finale value which will be partner name in this case