在日常开发Odoo的过程中,我们不免要用到Constraints,中文就是约束。 
首先我们来介绍下Odoo里面的两种Constraints。

SQL Constraints:就是添加一个数据库的约束。 
_sql_constraints是odoo的属性,是一个元祖的列表,每个元祖是一个数据库约束。元祖的第一个元素是约束名字,第二个元素是约束规则(postgresql约束规则),第三个参数是如果违反约束弹出来的警告信息。

    _sql_constraints = [
        ('name_description_check',
         'CHECK(name != description)',
         "The title of the course should not be the description"),

        ('name_unique',
         'UNIQUE(name)',
         "The course title must be unique"),
    ]

注意在使用SQL Constraints,需要确保当前数据库里面没有违反该约束的数据,如果有违反约束的数据在更新模块的时候系统日志里面会有警告信息,大家要注意这个。

Constraints:

    @api.constrains('instructor_id', 'attendee_ids')
    def _check_instructor_not_in_attendees(self):
        for r in self:
            if r.instructor_id and r.instructor_id in r.attendee_ids:
                raise exceptions.ValidationError("A session's instructor can't be an attendee")

odoo的Constraints,是通过装饰器@api.constrains(字段),每次记录修改的时候,如果包含了装饰器定义的字段就会触发下面的方法,所以需要在方法里面判断是否违反约束,如果违反,则通过raise异常来弹出警告框并阻止记录保存。使用odoo Constraints的时候就算是系统内已经有违反约束的记录也可以对新记录生效。

最新文章

  1. 转载:《.NET 编程结构》专题汇总(C#)
  2. 【leetcode】Search in Rotated Sorted Array II(middle)☆
  3. 刨根问底U3D---Vector3 你到底是蔬菜呢还是水果呢?
  4. JAVA基础知识之JDBC——离线RowSet
  5. JavaScript字符串常用操作函数之学习笔记
  6. angularjs中只显示选中的radio的值
  7. 解决Centos关闭You have new mail in /var/spool/mail/root提示
  8. 转来的 cuda makefile 写法学习
  9. Cassandra1.2文档学习解读计划——为自己鼓劲
  10. Tower of Hanoi问题
  11. Java 判断是否为汉字 判断是否为乱码 判断字符串是否为双整型数字 整数 数字
  12. 我的新顶级域名vell001.ml
  13. hdu1753java(大数相加)---BigDecimal类
  14. 多加注意对null和“”的处理
  15. tomcat支持php
  16. awk练习题-v参数
  17. Python内置函数(36)——reversed
  18. SSM-MyBatis-04:Mybatis中使用properties整合jdbc.properties
  19. 推荐一款关于MongoDB日志分析的工具--Mtools
  20. xtrabackup 2.4.3 BUG

热门文章

  1. VS启用IIS调试的方法及可能碰到的问题。
  2. 20160205.CCPP体系详解(0015天)
  3. android 单独编译某个模块
  4. 如何调试Android Framework?
  5. 使用git自动将子工程发布到百度开放云上
  6. nginx服务器防sql注入/溢出攻击/spam及禁User-agents
  7. Aptana 插件 for Eclipse 4.4
  8. Java 分割文件 注意事项
  9. linq性能剖析
  10. web前端笔试题