How to set default values with methods in Odoo?

半城伤御伤魂 提交于 2019-11-30 13:20:08

问题


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

回答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"



回答2:


A simpler version for the @ChesuCR answer:

def _get_default_name(self):
    return "test"

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


来源:https://stackoverflow.com/questions/31583328/how-to-set-default-values-with-methods-in-odoo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!