跳至内容
菜单
此问题已终结
4 回复
13517 查看

According to the documentation, it is possible to assign the result of a function as default value of a field. But is it possible to assign the value of another field of the same model as default value?

Example:

display_name = fields.Char(string='Display Name', default=name, translate=True)
形象
丢弃
最佳答案

Hi,

you can use

@api.onchange('<field whose value to be taken>')

def onchange_func(self):

if self.<field value to be taken>:

self.  <field to which you need value> = self.<field  whose value to be taken>

形象
丢弃
编写者

Thanks for your reply. How would my field definition look like then:

display_name = fields.Char(default=onchange_func(name)) ?

最佳答案

You can achieve that by this one-liner, note though that the value of the other field should be created afore:


display_name = fields.Char(string='Display Name', default=lambda self: self.name, translate=True)

形象
丢弃
相关帖文 回复 查看 活动
1
5月 22
4339
1
8月 19
4256
1
12月 21
6676
0
10月 21
3303
1
10月 21
4126