Search code examples
default-valuemany-to-oneodoo-9

How can I set default value for a many2one field in odoo 9?


I am new with Odoo-9.0c system and try to make some change to support the work. But I don't know how to set a specific value for many2one field ((selection type). The field also has been created through a custom module. what is the right way to solve this kind of problem?


Solution

  • With the new api, just specify which field attribute to change:

    field_name = fields.Many2one(default=a_function)
    

    From Odoo new api guide line

    Field inheritance

    One of the new features of the API is to be able to change only one attribute of the field:

    name = fields.Char(string='New Value')
    

    ...

    Fields Defaults

    Default is now a keyword of a field:

    You can attribute it a value or a function

    name = fields.Char(default='A name')
    

    or

    name = fields.Char(default=a_fun)
    

    ...

    def a_fun(self):  
        return self.do_something()