콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
7147 화면

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

관련 게시물 답글 화면 활동
@api.constrains 해결 완료
2
12월 23
3486
0
9월 23
1934
0
9월 23
1632
2
6월 25
11106
6
10월 23
21947