原始模块的退出字段上的sql_cosntraints

时间:2018-06-21 14:46:13

标签: odoo-8 odoo odoo-9

在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!"),
]

2 个答案:

答案 0 :(得分:2)

  • 请尝试使用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"))

答案 1 :(得分:1)

我将在模型if(changes.accountBalanceStatus.previousValue != changes.accountBalanceStatus.currentValue || changes.chart.previousValue != changes.chart.currentValue){ this.renderChart(); } 上添加约束,因为这是真正使用此信息(产品参考)的地方。但是product.product上的default_code仅在Odoo V10起可用。在Odoo V8和V9中,它是一个未存储的相关字段,因此在DB中则不是。因此,您必须在模型product.template上添加约束。

product.product

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

相关问题