sql_cosntraints从原始模块退出字段



在product.template中,有字段default_code。是否可以添加默认代码应该是唯一的sql_constraints。因为此代码不起作用。还是我需要覆盖服装模块中的default_code字段?

class ProductProduct(models.Model):
_inherit = 'product.template'

_sql_constraints = [
('code_uniq', 'unique (default_code)', "Default code already exists!"),
]
  • 请尝试使用Python约束,可能对您有用:
  • 在 Python 文件中导入这些行:

    from openerp.exceptions import ValidationError

  • 任何在你的类中写这个方法:

    @api.constrains('default_code') def _check_default_code(self): code = self.search([('default_code','=',self.default_code)]) if len(code) >1: raise ValidationError(_("Duplicate Record"))

我会在模型product.product上添加约束,因为这是真正使用这些信息(产品参考(的地方。但是default_codeproduct.template只能从Odoo V10开始工作。在 Odoo V8 和 V9 中,它是一个未存储的相关字段,因此不在数据库中。因此,您必须在模型product.product上添加约束。

class ProductProduct(models.Model):
_inherit = 'product.product'
_sql_constraints = [
('code_uniq', 'unique(default_code)', "Default code already exists!"),
]

重要提示:如果设置约束的模块在约束失败时进行了更新(例如,default_code实际上在 db 中两次(,它不会在 db 中创建 sql 约束。因此,您必须清理数据并再次更新模块,或者自己在数据库中创建约束。

最新更新