Search code examples
python-2.7odooodoo-8

How to set character limit in Text field ..?


My py and xml files code show below and i want to set 300 character limit for text field so what kind of changes needed in this code ?

.py Code :

description': fields.text('Description', required=True)

.Xml Code :

<field name="description"/>

Solution

  • Here I am explaining how to use constraints effectively.

    @api.constrains('your_field')
    @api.one
    def _check_your_field(self):
    
        if len(self.your_field) > 300:
            raise ValidationError('Number of characters must not exceed 300')
    

    Don't forget to import

    from odoo.exceptions import UserError, ValidationError