Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
8952 Lượt xem

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! 

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you for fast reply!