Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
4434 Zobrazení

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 ?

Avatar
Zrušit
Nejlepší odpověď

_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


Avatar
Zrušit
Nejlepší odpověď

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

Avatar
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
0
pro 24
784
1
bře 24
1761
2
pro 23
2502
1
čvn 23
11752
0
led 23
2519