Odoo Help


Constraint number of characters in a fields.Char

Nissan Pagotaisidro
on 11/3/15, 11:24 PM 1,348 views

Hello! Help me on how to constraint a number of characters in a fields.Char? 

and number must be minimum of 12 and maximum of 15 characters

here's my models.py

bir_id = fields.Char(size=15)

def _check_length(self, cr, uid, ids, context=None):
     for record in self.browse(cr, uid, ids):
        if record.bir_id < 12:
        return False
    return True
_constraints = [(_check_length, 'Error', ['bir_id'])]

when i enter less than 12 characters, there's no error. What do you think is the problem? 

Thank you! 


Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 11/3/15, 11:45 PM

Hi Nissan,

To check the length (number of characters in your string), you should use len(your_string).


if len(record.bir_id) < 12 or len(record.bir_id) > 15:
# raise exception or return False
return False

Hope this will help you.

Thank you for fast reply!

Nissan Pagotaisidro
on 11/4/15, 12:00 AM

Nice +1

Ankit H Gandhi(AHG)
on 11/4/15, 9:52 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 11/3/15, 11:24 PM
Seen: 1348 times
Last updated: 11/3/15, 11:59 PM