我开发了一个Odoo模块,用于向员工添加序列,工作非常完美。
我确实单击“创建”按钮并显示这些员工的序列,但仍然取消了“创建序列增量”。
class nhr(models.Model):
_inherit = 'hr.employee'
nhr = fields.Char(string='Nº de contacto', index=True, readonly=True, required=True,
default=lambda self: self.env['ir.sequence'].next_by_code('nhr.seq'))发布于 2016-11-15 17:51:07
它会增加序列号,因为在字段声明中设置了默认值。
为了避免此类问题,我们需要在create()方法中设置逻辑。
尝试使用以下代码:
nhr = fields.Char(string='Nº de contacto', index=True, readonly=True)
@api.model
def create(self, vals):
vals['nhr'] = self.env['ir.sequence'].next_by_code('nhr.seq')
return super(nhr, self).create(vals)注意:
重新启动Odoo服务器并升级自定义模块。
发布于 2016-11-16 07:41:11
在这种情况下,当创建nhr记录的事务回滚时,@Odedra的解决方案无法工作。在这种情况下,底层序列实现仍将增加。
您可以使用序列的“无缝隙”实现(它是ir.sequence模型中的一个字段)来确保所有数字都是连续的。然而,这需要付出巨大的代价,因为实现使用全局锁工作,这将序列化您的记录的创建。
https://stackoverflow.com/questions/40616032
复制相似问题