Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4748 Lượt xem

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 ?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

_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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 12 24
1414
1
thg 3 24
2454
@api.constrains Đã xử lý
2
thg 12 23
3444
1
thg 6 23
12462
0
thg 1 23
3154