Chen Jian

导航

 

内容摘自:http://blog.csdn.net/sz_bdqn/article/details/8785483

_constraints

_constraints可以灵活定义OpenERP对象的约束条件,当创建或更新记录时,会触发该条件,如果条件不符合,则弹出错误信息,拒绝修改。

_constraints的定义格式:

[(method, 'error message', list_of_field_names), ...]

method: 是对象的方法,该方法的格式为:

error message: 不符合检查条件(method返回False)时的错误信息。

list_of_field_names: 字段名列表,这些字段的值会出现在error message中。通常列出能帮助用户理解错误的字段。

_constraints的例子:

程序代码:

def _constraint_sum(self, cr, uid, ids):

    cr.execute('SELECT a.currency_id

        FROM account_move m, account_move_line l, account_account a

        WHERE m.id=l.move_id AND l.account_id=a.id AND m.id IN ('+','.join(map(str, ids))+')

        GROUP BY a.currency_id')

    if len(cr.fetchall()) >= 2:

        return True

    cr.execute('SELECT abs(SUM(l.amount))

        FROM account_move m LEFT JOIN account_move_line l ON (m.id=l.move_id)

        WHERE m.id IN ('+','.join(map(str, ids))+')')

    res = cr.fetchone()[0]

    return res < 0.01

 

_constraints = [

    (_constraint_sum, 'Error: the sum of all amounts should be zero.', ['name'])

    ]

_sql_constraints 和 _order

    _sql_constraints定义数据表的约束条件,其格式如下例所示。

_sql_constraints = [

        ('code_company_uniq', 'unique (code,company_id)', 'The code of the account must be unique per company !')

    ]

本例的_sql_constraints会在数据表中增加下述约束:

CONSTRAINT ObjectName_code_company_uniq UNIQUE(code, company_id)
posted on 2014-08-27 14:10  Chen Jian  阅读(437)  评论(0编辑  收藏  举报