How to set default values with methods in Odoo?

25,034

Solution 1

You can use a lambda function like this:

name = fields.Char(
    string='Name',
    default=lambda self: self._get_default_name(),
)

@api.model
def _get_default_name(self):
    return "test"

Solution 2

A simpler version for the @ChesuCR answer:

def _get_default_name(self):
    return "test"

name = fields.Char(
    string='Name',
    default=_get_default_name,
)
Share:
25,034
Jay Venkat
Author by

Jay Venkat

Updated on April 07, 2020

Comments

  • Jay Venkat
    Jay Venkat about 4 years

    How to compute the value for default value in object fields in Odoo 8 models.py

    We can't use the _default attribute anymore in Odoo 8.

    field_name = fields.datatype(
        string=’value’, 
        default=compute_default_value
    )
    

    In the above field declaration, I want to call a method to assign default value for that field. For example:

    name = fields.Char(
        string='Name', 
        default= _get_name()
    )