首页 > 解决方案 > 如何打印动态sql约束错误语句

问题描述

我正在使用 odoo 11,我想更改以下 SQL 约束的错误状态并打印一条动态错误语句,该语句在错误消息中显示字段“数字”。

from odoo import api, fields, models

 class ValidateMessageUnique(models.Model):

  _inherit = 'account.invoice'

  _sql_constraints = [
    ('number_uniq', 'unique(number, company_id, journal_id, type)', 'Invoice Number should be unique per Company!' )
  ]

我希望 ErrorMessage 是'Invoice Number'+number+ 'should be the unique per company'.

标签: pythonsqlpython-3.xodooodoo-11

解决方案


不,无法在_sql_constraint错误消息中打印动态消息。

您必须知道,您在其中写的任何消息_sql_constraint都不是可编辑的字母。

如果您希望该消息动态,则有一种方法,那么您需要检查发生此类错误时的所有可能性,然后需要使用动态消息内容发出警告。希望你能好好理解。


推荐阅读