我有一种形式的字段,

Name :
First Name:
Second Name:
Last Name:

我写了创建函数,当我们输入名字,名字,姓氏时,它将设置名字
First Name: Test First
Second Name: Test Second
Last Name: Test Third

它会产生
Name : Test First Test Second Test Third

这是因为创建了函数。
@api.model
def create(self, vals):
    child = vals.get('child_first_name')
    child_middle = vals.get('child_middle_name') or ' '
    child_last = vals.get('child_last_name') or ' '
    if child:
        vals['name'] = self.env['ir.sequence'].next_by_code('application_form')
        vals['child_name'] = child + ' ' + child_middle + ' ' + child_last
    return super(application_form, self).create(vals)

当我编辑任何这些字段时
First Name
Second Name
Last Name

它应该再次设置名称。

请建议一个用好方法编写函数。

以同样的方式:

问题2 :

有一个布尔字段。

以供参考:

我在 create 方法中编写了要生成的序列号。
如果我取消选中该布尔字段,则会生成另一组序列号。

请建议为两者编写函数。

对于写上面的问题
@api.multi
def write(self, vals):
    application = self.browse(self.id)
    if vals.has_key('child_first_name'):
        fname = vals.get('child_first_name') or ' '
    else :
        fname = application.child_first_name
    if vals.has_key('child_middle_name'):
        mname = vals.get('child_middle_name') or ' '
    else :
        mname = application.child_middle_name
    if vals.has_key('child_last_name'):
        lname = vals.get('child_last_name') or ' '
    else :
        lname = application.child_last_name
    full_name = fname + ' ' + mname + ' ' + lname
    vals.update({'child_name':full_name})
    return super(application_form, self).write(vals)

最佳答案

这是一个如何实现此目的的示例

@api.multi
def write(self, vals):
    # checks that new field value
    # is not equal to old one
    def _is_changed(name):
        return name in vals and self[name] != vals[name]

    # returns new field value if present
    # otherwise return old value if present
    # else return default value
    def _get_field(name, default=''):
        return vals.get(name, self[name]) or default

    # choose which sequence to use based on boolean seq_flag
    seq = 'some_seq' if _get_field('seq_flag', False) else 'application_form'

    # if one of name parts was changed
    if _is_changed('child_first_name') or \
       _is_changed('child_middle_name') or \
       _is_changed('child_last_name'):
        vals['name'] = self.env['ir.sequence'].next_by_code(seq)
        name_parts = [
            _get_field('child_first'),
            _get_field('child_middle_name'),
            _get_field('child_last_name')
        ]
        # write only non-empty name parts
        vals['child_name'] = " ".join([_ for _ in name_parts if _])
    return super(application_form, self).write(vals)

您也可以使用计算出的child_last_name字段和 @api.depends 解决问题的第一部分
child_name = fields.Char(compute='_compute_name')

@api.one
@api.depends('child_first', 'child_middle_name', 'child_last_name')
def _compute_upper(self):
    name_parts = [
        self.child_first,
        self.child_middle_name,
        self.child_last_name
    ]
    self.child_name = " ".join([_ for _ in name_parts if _])

关于python-2.7 - Odoo:Odoo 8中的写入功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39267298/

10-11 00:15