跳至内容
菜单
此问题已终结
2 回复
7129 查看

Hi guys,

I want to check _sql_constraints of  a model using function

Example , I have a function

@api.one
def check_constraint(self)
     # CODE TO CHECK
     return True # If ok

     return False # If not ok

The below constraint must raise an error if the function returns False

_sql_constraints = [ .................]

I know It is possible  by overriding the create method. But I forgot the correct way

Thanks in advance

 

形象
丢弃
最佳答案

Use python constraints instead

https://www.odoo.com/documentation/9.0/reference/orm.html#openerp.api.constrains

https://www.odoo.com/documentation/9.0/reference/orm.html#openerp.models.Model._constraints

形象
丢弃
编写者

Thanks Axel , It is working

最佳答案

Hi Shameem,

you have two ways.

one way:

std_id = fields.Char(string="Student ID", required=True)   

_sql_constraints = [('std_id_uniq', 'unique(std_id)', 'This ID already exists !')]


Another way using api:

mark1 = fields.Integer(string="Total Mark")

@api.constrains('mark1')   

def _check_mark1(self):
        if self.mark1 == 0:
            raise ValidationError("Please enter the marks !")


Also you can check any logical operations inside the constrains function.

Hope this may help you


形象
丢弃
编写者

Thanks Nilmar,

It is working while adding @api.one

Same answer of Axel , you explained more

You have an upvote

Good. Keep going :)

相关帖文 回复 查看 活动
2
12月 23
3448
0
9月 23
1924
0
9月 23
1613
2
6月 25
11066
6
10月 23
21924