如何在odoo新API中覆盖字段的默认功能 [英] How overwrite default function of a field in odoo new api

查看:79
本文介绍了如何在odoo新API中覆盖字段的默认功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个调用函数以获取默认值的字段(在project_forecast模块中):

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)

我创建了另一个模块来继承它并更改了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

推荐答案

您正在直接在字段定义上链接方法.因此,仅覆盖该方法是不够的.只需使用default作为唯一参数再次定义字段user_id,然后使用新方法即可:

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:

原始模型:

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)

继承的模型:

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)

这篇关于如何在odoo新API中覆盖字段的默认功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆