问题
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