Odoo Help

Welcome!

This community 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.

0

Constraint number of characters in a fields.Char

By
Nissan Pagotaisidro
on 11/3/15, 11:24 PM 465 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! 

2

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 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).

Ex:

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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