Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
4797 Переглядів

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

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
0
груд. 24
1447
1
бер. 24
2485
@api.constrains Вирішено
2
груд. 23
3544
1
черв. 23
12498
0
січ. 23
3174