_constraints的例子:
程序代码: [url=][选择][/url]
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)
_order在对象的search或read方法中的select语句上加上"Order"子句,如 _order = 'name desc, account_id',对应SQL文的Order子句:order by name desc, account_id。