zoukankan      html  css  js  c++  java
  • Openerp约束句型

    内容摘自: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)
  • 相关阅读:
    asp.net 对母版页的控件事件
    treeview操作集合
    使用GAppProxy时安全证书无效的解决办法
    向Excel模板中添加数据
    C# 重写 winform 关闭按钮
    完整ASP.Net Excel导入程序(支持2007)
    随笔二则
    标记枚举(flags)的使用
    System.Reflection.Missing.Value与Type.Missing
    Windows下Android源码下载方法
  • 原文地址:https://www.cnblogs.com/chjbbs/p/3939344.html
Copyright © 2011-2022 走看看