gpt4 book ai didi

python-2.7 - 奥多 : Write function in Odoo 8

转载 作者:行者123 更新时间:2023-12-02 03:12:15 25 4
gpt4 key购买 nike

我有一个带有字段的表单,

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

这是因为 create 函数。

@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:

有一个 bool 字段。

供引用:

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

请为两者建议编写函数。

为了写上面的问题

@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 - 奥多 : Write function in Odoo 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com