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

I have a fields.Char attribute “identification_id”, That field must have  11 characters. How can I add a _sql_constraints to check the length of that field and ensure it should be 11 ?

아바타
취소
베스트 답변

_sql_constraints = [
        (
            'identification_id_len_check',
            'check (LENGTH(identification_id) = 11)',
            ('identification_id should be 11 symbols')
        ),
    ]
SQL constraints are faster in comparison to api.constrains. 
Make sure that none of your existing records have different size. Otherwise, constrain would not be created


아바타
취소
베스트 답변

Hi,

If it is not mandatory to for _sql_constraints, try to achieve the same using the constrains.

@api.constrains('identification_id')
def check_identification_id(self):
for rec in self:
if len(rec.identification_id) != 11:
raise ValidationError(_('Must be 11 Characters'))


See : How To Add Constrains For A Field in Odoo12


Thanks

아바타
취소
관련 게시물 답글 화면 활동
0
12월 24
971
1
3월 24
2001
@api.constrains 해결 완료
2
12월 23
2836
1
6월 23
12006
0
1월 23
2785