当前位置:网站首页>odoo的约束
odoo的约束
2022-07-13 19:18:00 【姜振建 15954039008】
约束有两种实现方法:
SQL 约束是确保数据一致性的有效方法。但如需进行更复杂的检查,便需要 Python 约束。
1、sql约束
SQL 约束是通过模型属性定义的 _sql_constraints(name, sql_definition, message)。
如下代码:
_sql_constraints = [
(‘code_company_uniq’, ‘unique (code,company_id)’, ‘账号必须唯一!’)
]
实现效果:
金额不能小于0的实现效果:
2、python约束
语法:@api.constrains(‘date_end’)
如实现不得低于预期价格 90% 的报价的效果:
如要实现日期判断(结束日期不允许是今天之前的),代码如下:
from odoo.exceptions import ValidationError
...
@api.constrains('date_end')
def _check_date_end(self):
for record in self:
if record.date_end < fields.Date.today():
raise ValidationError("结束日期不允许是今天之前的")
# all records passed the test, don't return anything
总结:
SQL 约束通常比 Python 约束更有效。当性能很重要时,我更喜欢 SQL约束 而不是 Python 约束。
边栏推荐
- Extract the language you need from the multilingual data set and save it as CSV
- Hcip second experiment
- Hj8 consolidated statement record hj08
- 管理学的发展
- LeetCode 2155. All subscripts with the highest score in the group
- 第三讲:取硬币
- Common mailbox access protocols
- HCIP第三天笔记
- Interprocess communication (very practical)
- This article takes you to understand what tensors are in deep learning, how their operations are, how to understand tensors, and the dimensions of tensors, which are easy to understand
猜你喜欢
随机推荐
音视频学习(七)——AAC音频码流
[server data recovery] a data recovery case in which a brand of MSA SAN storage RAID5 is paralyzed and the upper Lun cannot be used
渲染流程,代码是如何变为页面的(二)
首届人工智能安全大赛启动报名 三大赛题等你来战
HCIP第四天笔记
QT+VS 工程在 Release/Debug 文件夹下导入相关 DLL (非常实用)
Hcip day 5 experiment
Message Mechanism of dtcloud (1)
先序和中序遍曆序列確定一顆二叉樹(還原二叉樹)
Is it safe to open an account for stock speculation through the online account manager?
Anonymous pipeline principle and detailed explanation (very practical)
密码密钥硬编码检查
LeetCode 1584. 连接所有点的最小费用
JMeter 21 day clock in day01
Why do more and more people want to take the PMP project management certification?
c语言 寄存器技巧 (struct 和 union)
babylon.js高度图
wait 和 waitpid 的区别
How to set various displays in 3dmax2021?
先序和中序遍历序列确定一颗二叉树(还原二叉树)








