odoo 开发入门教程系列-约束(Constraints)

简介: odoo 开发入门教程系列-约束(Constraints)

约束(Constraints)

上一章介绍了向模型中添加一些业务逻辑的能力。我们现在可以将按钮链接到业务代码,但如何防止用户输入错误的数据?例如,在我们的房地产模块中,没有什么可以阻止用户设置负预期价格。

odoo提供了两种设置自动验证恒定式的方法:Python约束 and SQL约束

SQL

参考:与此主题相关的文档可以查看 ModelsPostgreSQL文档

我们通过模型属性_sql_constraints来定义SQL约束,该属性被赋值为一个包含三元组(name, sql_definition, message)的列表,其中name为一个合法的SQL约束名称, sql_definition表约束表达式,message为错误消息。

一个简单的示例

class AccountAnalyticDistribution(models.Model):
    _name = 'account.analytic.distribution'
    _description = 'Analytic Account Distribution'
    _rec_name = 'account_id'
    account_id = fields.Many2one('account.analytic.account', string='Analytic Account', required=True)
    percentage = fields.Float(string='Percentage', required=True, default=100.0)
    name = fields.Char(string='Name', related='account_id.name', readonly=False)
    tag_id = fields.Many2one('account.analytic.tag', string="Parent tag", required=True)
    _sql_constraints = [
        ('check_percentage', 'CHECK(percentage >= 0 AND percentage <= 100)',
         'The percentage of an analytic distribution should be between 0 and 100.')
    ]

一个简单的示例--唯一约束

class BlogTagCategory(models.Model):
    _name = 'blog.tag.category'
    _description = 'Blog Tag Category'
    _order = 'name'
    name = fields.Char('Name', required=True, translate=True)
    tag_ids = fields.One2many('blog.tag', 'category_id', string='Tags')
    _sql_constraints = [
        ('name_uniq', 'unique (name)', "Tag category already exists !"),
    ]

练习--添加SQL约束

添加以下约束到对应模型:

  • 房产预期价格必须为正数
  • 房产售价必须为正数
  • 报价必须为正数
  • 房产标签名称和类型名称必须唯一

使用-u estate选项重新启动服务器以查看结果。请注意,可能存在阻止设置SQL约束的数据。可能会弹出类似以下内容的错误消息:

ERROR rd-demo odoo.schema: Table 'estate_property_offer': unable to add constraint 'estate_property_offer_check_price' as CHECK(price > 0)

例如,如果某些报价的价格为零,则无法应用约束。可以删除、修正有问题的数据以应用新的约束。

修改odoo14\custom\estate\models\estate_property.py,添加SQL约束

_sql_constraints = [
        ('check_expected_price', 'CHECK(expected_price > 0)', 'expected price should be positive.'),
        ('check_selling_price', 'CHECK(selling_price > 0)', 'selling price should be positive.')
    ]

注意:当selling_price为null时,也通过CHECK(selling_price > 0)校验的

修改odoo14\custom\estate\models\estate_property_tag.py,添加SQL约束

_sql_constraints = [('check_tag', 'unique(name)', 'Tag name must be unique !')]

修改odoo14\custom\estate\models\estate_property_type.py,添加SQL约束

_sql_constraints = [('check_name', 'unique(name)', 'Type name must be unique !')]

重启服务验证

预期效果动画:https://www.odoo.com/documentation/14.0/zh_CN/_images/sql_01.gif

https://www.odoo.com/documentation/14.0/zh_CN/_images/sql_02.gif

Python

参考: 主题关联文档可查看constrains().

SQL约束是确保数据一致性的有效方法。然而,可能需要进行更复杂的检查,这需要Python代码。在这种情况下,我们需要一个Python约束。

Python约束定义为用 constrains()修饰的方法,并在记录集上调用。修饰符指定约束中涉及哪些字段。当修改这些字段中的任何字段时,将自动计算约束。如果不满足该方法的恒定式,则该方法将引发异常:

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("The end date cannot be set in the past")
    # all records passed the test, don't return anything

一个简单的示例

@api.constrains('quantity')
    def check_quantity(self):
        for quant in self:
            if quant.location_id.usage != 'inventory' and quant.lot_id and quant.product_id.tracking == 'serial' \
                    and float_compare(abs(quant.quantity), 1, precision_rounding=quant.product_uom_id.rounding) > 0:
                raise ValidationError(_('The serial number has already been assigned: \n Product: %s, Serial Number: %s') % (quant.product_id.display_name, quant.lot_id.name))

练习--添加Python约束

添加售价不能低于预期价格90%的约束

提示: 报价生效前,保持售价为0。你需要对校验进行微调,以便把这个考虑在内。

警告

当和浮点数打交道时,总是使用从 odoo.tools.float_utils导入的float_compare()float_is_zero()方法

确保每次售价或者预期价格改变时,自动触发约束

修改odoo14\custom\estate\models\estate_property.py

导入 ValidationError

from odoo.exceptions import ValidationError

最末尾添加以下代码

@api.constrains('selling_price', 'expected_price')
    def _check_selling_price(self):
        for record in self:
            if record.selling_price < self.expected_price * 0.9:
                raise ValidationError("selling price can`t not lower then 90 percent of expected price")

重启服务,浏览器中验证

预期效果动画:https://www.odoo.com/documentation/14.0/zh_CN/_images/python.gif

SQL约束通常比Python约束更效率。当性能很重要时,总是首选SQL约束而不是Python约束。

目录
相关文章
|
10月前
|
IDE 开发工具 索引
在Python中安装第三方库
在Python中安装第三方库
2290 30
|
10月前
|
Kubernetes 调度 容器
使用Kmesh作为阿里云服务网格ASM Sidecarless模式数据面
阿里云服务网格ASM支持Sidecar和Sidecarless两种模式,其中Sidecarless模式如Istio Ambient、ACMG和Kmesh等,可减少延迟和资源消耗。Kmesh基于eBPF技术,通过内核空间拦截流量,结合Waypoint Proxy处理L7流量,实现高效的服务治理。本文介绍了如何在阿里云ACK集群中部署Kmesh并连接ASM控制面,包括安装步骤、检查服务状态和流量调度示例。
|
12月前
|
关系型数据库 MySQL 数据库
MYSQL索引的分类与创建语法详解
理解并合理应用这些索引类型,能够有效提高MySQL数据库的性能和查询效率。每种索引类型都有其特定的优势,适当地使用它们可以为数据库操作带来显著的性能提升。
501 3
|
机器人 测试技术 C++
ModbusCRC16校验 示例代码
本文提供了ModbusCRC16校验的示例代码,包括计算CRC、添加CRC校验位、删除CRC校验位和比较CRC校验位等四种常用函数的实现,以及一个完整的示例代码。这些代码在Ubuntu20.04环境下经过测试并确认有效。示例代码采用C++编写,展示了如何使用这些函数来处理Modbus通信中的数据校验。
598 2
|
安全 网络协议 网络安全
常见网络攻击方式及防御方法
网络安全威胁的不断演变和增长,网络攻击的种类和数量也在不断增加,攻防对抗实战演练在即,让我们一起了解一下常见网络攻击方式及防御方法。
831 0
|
数据库 关系型数据库 MySQL
DTS库支持多种数据源类型
【6月更文挑战第2天】DTS库支持多种数据源类型
237 3
|
XML JSON 程序员
程序员必知:常用天气预报API接口整理(转)
程序员必知:常用天气预报API接口整理(转)
425 0
|
Ubuntu 关系型数据库 数据库
|
存储 Ubuntu Linux
Ubuntu 24.04 LTS系统安装Cockpit-Podman过程步骤
在 Ubuntu 24.04 上通过命令行使用容器可能有点困难,特别是对于初学者来说,因此,为了方便,我们可以在 Ubuntu 24.04 服务器上安装 Cockpit 和 Cockpit-podman 模块。接下来趣云笔记(www.ecscoupon.com)简单介绍Ubunt安装和配置Cockpit-Podman的步骤。
1486 0
|
Ubuntu Linux 网络安全
Linux(31)Rockchip RK3568 Ubuntu22.04上部署 Docker: 问题与解决方案
Linux(31)Rockchip RK3568 Ubuntu22.04上部署 Docker: 问题与解决方案
1176 0