Skip to Content
Menu
This question has been flagged
2 Replies
7814 Views

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

Avatar
Discard
Best Answer

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'))
 

Avatar
Discard
Best Answer

You can use parameter size = 4 in field definition

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

Avatar
Discard