从odoo中的只读字段保存值

Save value from readonly field in odoo

我在表单上有 3 个字段字段总和是只读的。单击“从只读字段保存数据”后,字段未存储在数据库中。

示例:

class myClass(models.Model):

number_1 = fields.Integer(store=True,default=0)
number_2 = fields.Integer(store=True,default=0)
sum = fields.Integer(store=True)

@api.onchange('number_1','number_2')
def compute_sum(self):
    total = self.number_1 + self.number_2
    self.sum = total

在这种情况下,我更喜欢计算域。只读字段不会被公式更改保存(例如更改事件)。在 Odoo 11 中,针对视图中的这种行为引入了一个新选项 force_save,但之前的版本没有此选项(社区模块除外,例如 web_readonly_bypass by OCA)。

计算域的解决方案:

class myClass(models.Model):

    number_1 = fields.Integer()
    number_2 = fields.Integer()
    sum = fields.Integer(compute="compute_sum", store=True)

    @api.depends('number_1','number_2')
    @api.multi
    def compute_sum(self):
        for record in self:
            total = record.number_1 + record.number_2
            record.sum = total

无需更改视图定义。并且在普通 Integer 字段中也不需要 store 参数。默认值 0 已经是 Integer 上的默认值,因此需要定义它。

您不必在 sum 的视图中显式定义 readonly,因为默认情况下,没有逆向方法的计算字段是只读的。

希望下面的代码对您有所帮助

在我的例子中,总年份字段是只读的,并且基于 'Date of birth' 总年份将被更新

使用onchange方法,可以在字段上获取总年份但是当保存该记录时总年份字段设置为空白

解决方案:-

创建总年的新虚拟字段并在原始字段上设置该虚拟字段值

示例:-

Python 文件

total_year = fields.Float()

total_year_copy = fields.Float()

从日期时间导入日期

Onchange 方法

@api.onchange('dob') def onchange_dob(自我): 今天 = date.today() self.total_year = self.total_year_copy = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

创建方法

@api.model

def create(self, vals):

如果 'total_year_copy' 在 vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).create(vals)

写法

@api.multi

def write(self, vals):

如果 'total_year_copy' 在 vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).write(vals)

Xml 文件

字段名称="total_year" readonly="1"

字段名称="total_year_copy" invisible="1"

希望这可以帮助您保存只读记录

此致,

Ankit H 甘地

from openerp import models, fields, api,exceptions

class your_model(models.Model):
    _inherit = 'your.model'

    field_1 =fields.Many2one('model.a', required="True", string="Field 1")
    field_2 = fields.Integer(string = 'Field 2')

    # onchange of field_1 we are populating value in field_2

    @api.onchange('field_1')
    def _onchange_field_1(self):
        if field_1:
            # your logic goes here to get value for field_2
            self.field_2 = some_value

    # As we know that when we get the value on onchange method, 
    # the desired field convert into readonly mode and its value does not save into the database
    # to save the value into the database we are going to override create and write method and in that method we explicitly save the value

    @api.model
    def create(self,vals):
        if self.field_2:
            vals['field_2']=self.field_1
        res = super(your_model, self).create(vals)
        return res

    @api.multi
    def write(self,vals):
        if not self.field_2:
            vals['field_2'] = self.field_1
        res = super(your_model, self).write(vals)
        return res

参考:Save readonly field value in database