diff --git a/doc/ref/models/fields.rst b/doc/ref/models/fields.rst index 34bba5e11c..f191399e23 100644 --- a/doc/ref/models/fields.rst +++ b/doc/ref/models/fields.rst @@ -288,6 +288,7 @@ The field is ordered using the similarity with the context value from the key The maximum length (in characters) of the field. The size is enforced at the storage level and in the client input. + The value can be a :class:`~trytond.pyson.PYSON` statement. .. attribute:: Char.translate diff --git a/trytond/model/fields/char.py b/trytond/model/fields/char.py index 5b41309ff5..d42b2754ef 100644 --- a/trytond/model/fields/char.py +++ b/trytond/model/fields/char.py @@ -57,7 +57,10 @@ class Char(FieldTranslate): @property def _sql_type(self): - return 'VARCHAR(%s)' % self.size if self.size else 'VARCHAR' + if isinstance(self.size, int): + return 'VARCHAR(%s)' % self.size + else: + return 'VARCHAR' def set_rpc(self, model): super(Char, self).set_rpc(model) diff --git a/trytond/tests/field_char.py b/trytond/tests/field_char.py index 1214027759..44c22ab339 100644 --- a/trytond/tests/field_char.py +++ b/trytond/tests/field_char.py @@ -3,6 +3,7 @@ from trytond.model import ModelSQL, fields from trytond.pool import Pool +from trytond.pyson import Eval class Char(ModelSQL): @@ -33,6 +34,15 @@ class CharSize(ModelSQL): char = fields.Char("Char", size=5) +class CharSizePYSON(ModelSQL): + "Char PYSON Size" + __name__ = 'test.char_size_pyson' + char = fields.Char( + "Char", size=Eval('size', 0), + depends=['size']) + size = fields.Integer("Size") + + class CharTranslate(ModelSQL): 'Char Translate' __name__ = 'test.char_translate' @@ -68,6 +78,7 @@ def register(module): CharDefault, CharRequired, CharSize, + CharSizePYSON, CharTranslate, CharUnaccentedOn, CharUnaccentedOff, diff --git a/trytond/tests/test_field_char.py b/trytond/tests/test_field_char.py index e8b63c5942..61e77054e4 100644 --- a/trytond/tests/test_field_char.py +++ b/trytond/tests/test_field_char.py @@ -7,7 +7,7 @@ from sql import Literal from trytond import backend from trytond.model.exceptions import ( - ForbiddenCharValidationError, RequiredValidationError) + ForbiddenCharValidationError, RequiredValidationError, SizeValidationError) from trytond.pool import Pool from trytond.tests.test_tryton import ( ExtensionTestCase, activate_module, with_transaction) @@ -422,6 +422,29 @@ class FieldCharTestCase(unittest.TestCase, CommonTestCaseMixin): 'char': "foobar", }]) + @with_transaction() + def test_create_size_pyson_valid(self): + "Test create char with PYSON size" + Char = Pool().get('test.char_size_pyson') + + char, = Char.create([{ + 'char': "Test", + 'size': 5, + }]) + + self.assertEqual(char.char, "Test") + + @with_transaction() + def test_create_size_pyson_invalid(self): + "Test create char with invalid PYSON size" + Char = Pool().get('test.char_size_pyson') + + with self.assertRaises(SizeValidationError): + Char.create([{ + 'char': "foobar", + 'size': 5, + }]) + @with_transaction() def test_create_invalid_char(self): "Test create char with invalid char"