How overwrite default function of a field in odoo new api

不问归期 提交于 2019-12-07 22:30:00

问题


I have a field that call a function to get default value (in module project_forecast):

 def default_user_id(self):
    return self.env.user if ("default_user_id" not in self.env.context) else self.env.context["default_user_id"]

user_id = fields.Many2one('res.users', string="User", required=True,
                          default=default_user_id)

I create another module to inherit it and change default value of field user_id but it's not working without any error in log, how can I resolve this ?

def default_user_id(self):
    return False

回答1:


You're linking a method directly on field definition. So overriding the method isn't enough anymore. Just define the field user_id again with default as the only parameter and ofc your new method:

The original model:

class MyModel(models.Model):
    _name = "my.model"

    def default_user_id(self):
        return self.env.context.get("default_user_id", self.env.user)

    user_id = fields.Many2one(
        comodel_name='res.users', string="User", required=True,
        default=default_user_id)

The inherited model:

class MyModelInherit(models.Model):
    _inherit = "my.model"

    def default_user_id(self):
        return self.env['res.users']

    user_id = fields.Many2one(default=default_user_id)



回答2:


Please check this

user_id = fields.Many2one('res.users', string='User',readonly=True, default=lambda self: self.env.user)


来源:https://stackoverflow.com/questions/42529872/how-overwrite-default-function-of-a-field-in-odoo-new-api

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