Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
7154 มุมมอง

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 :)

Related Posts ตอบกลับ มุมมอง กิจกรรม
@api.constrains แก้ไขแล้ว
2
ธ.ค. 23
3491
0
ก.ย. 23
1938
0
ก.ย. 23
1635
2
มิ.ย. 25
11113
6
ต.ค. 23
21960