【问题标题】:odoo validate by databaseodoo 通过数据库验证
【发布时间】:2021-04-26 10:10:50
【问题描述】:

我想在字段 customer_id 和 project_product_id 一起插入并检查数据库时进行验证,如果两者与数据库相同则显示错误

我的py代码是这样的

_name='crm.project'
customer_id = fields.Many2one('res.partner','Customer')
@api.multi 
@api.constrains('customer_id','project_product_id')
def _check_total_value(self):
    target_list = []
    get_customer_id = self.customer_id
    get_project_product_id = self.project_product_id
self.env.cr.execute('''
                            select * 
                            from crm_project 
                            where customer_id = %s 
                                AND project_product_id = %s
                                
    ''',(get_customer_id,get_project_product_id))
    for target in self.env.cr.dictfetchall():
        target_list.append(target)
    if target_list:
        raise Warning("data duplicate")  

【问题讨论】:

    标签: odoo odoo-8 odoo-10 odoo-9


    【解决方案1】:

    检查数据库,如果两者与数据库相同则显示错误

    那么您的数据库中显然需要UNIQUE constraint

    不要在你的 python 代码中检查这些东西 - 当你在客户端应用程序中检查数据有效性时,有几十种情况可能会出错,而不是在数据库中等级。 More info.

    这就是make a database-level unique constraint in Odoo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 2014-01-12
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多