Python 2.7 Odoo:在Odoo 8中写入函数

Python 2.7 Odoo:在Odoo 8中写入函数,python-2.7,openerp,odoo-8,Python 2.7,Openerp,Odoo 8,我有一个带字段的表单 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):

我有一个带字段的表单

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)
您还可以使用computed
child\u last\u name
字段来解决问题的第一部分

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 _])

谢谢你编辑Hanks vsminkov。我有一个布尔字段示例:供参考。当我检查布尔字段序列号是否为AM/001时,当我取消检查布尔字段序列号是否为ORD/001时。假设序列号被创建为ORD/001,我将去编辑那个布尔字段,然后它应该是am/001。它应该只更改一次。因为每次我编辑该表单中的其他字段时,sequnce也将增加