Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
8411 Vizualizări

Hi,

I have integer field and i want to limite the number of digit that can be enter if not it should give an error message

for exemple limite numbers in 4 digit

i'm using odoo 10.

Regards

Imagine profil
Abandonează
Cel mai bun răspuns

You can do it using a constraint on the field.

This will prevent creating or updating a record with a value for the field 'number' that violates the constraint

from odoo.exceptions import UserError, RedirectWarning, ValidationError

#field
number = fields.Integer(string = "My Number")

#constraint
@api.constrains('number')
@api.one
def _check_number(self):
    number = self.number
    if number and len(str(abs(number))) > 4:
raise ValidationError(_('Number of digits must on exceed 4'))
 

Imagine profil
Abandonează
Cel mai bun răspuns

You can use parameter size = 4 in field definition

example -> number = fields.Integer(string = "My Number", size=4)

Imagine profil
Abandonează