Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
4436 Widoki

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 ?

Awatar
Odrzuć
Najlepsza odpowiedź

_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


Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
gru 24
799
1
mar 24
1785
@api.constrains Rozwiązane
2
gru 23
2509
1
cze 23
11789
0
sty 23
2549