Support instantiate model with _parent_ of _parent_

This is a valid depends value for a field but the __init__ method lose the
second _parent_ once it has been instantiated a first time.
Setting _parent_ value must be done recursively.

issue6441
review40261002
This commit is contained in:
Cédric Krier committed 2017-04-19 10:31:45 +02:00
1 parent 12824d6457
commit f7c1de84c4
4 files changed
+63 -4

No files matched your search

+11 -4
View File
@@ -359,14 +359,21 @@ class Model(WarningErrorMixin, URLMixin, PoolBase):
setattr(self, name, value)
else:
parent_values[name] = value
for name, value in parent_values.iteritems():
def set_parent_value(record, name, value):
parent_name, field = name.split('.', 1)
parent_name = parent_name[8:] # Strip '_parent_'
parent = getattr(self, parent_name, None)
parent = getattr(record, parent_name, None)
if parent is not None:
setattr(parent, field, value)
if not field.startswith('_parent_'):
setattr(parent, field, value)
else:
set_parent_value(parent, field, value)
else:
setattr(self, parent_name, {field: value})
setattr(record, parent_name, {field: value})
for name, value in parent_values.iteritems():
set_parent_value(self, name, value)
self._init_values = self._values.copy()
else:
self._values = None
+3
View File
@@ -113,6 +113,9 @@ def register():
BinaryRequired,
BinaryFileStorage,
Model,
ModelParent,
ModelChild,
ModelChildChild,
Singleton,
URLObject,
ModelStorage,
+21
View File
@@ -6,6 +6,7 @@ from trytond.transaction import Transaction
__all__ = [
'Model',
'ModelParent', 'ModelChild', 'ModelChildChild',
'Singleton', 'URLObject',
'ModelStorage', 'ModelStorageRequired', 'ModelStorageContext',
'ModelSQLRequiredField', 'ModelSQLTimestamp', 'ModelSQLFieldSet',
@@ -23,6 +24,26 @@ class Model(ModelSQL):
name = fields.Char('Name')
class ModelParent(Model):
"Model Parent"
__name__ = 'test.model_parent'
name = fields.Char("Name")
class ModelChild(Model):
"Model Child"
__name__ = 'test.model_child'
name = fields.Char("Name")
parent = fields.Many2One('test.model_parent', "Parent")
class ModelChildChild(Model):
"Model Child Child"
__name__ = 'test.model_child_child'
name = fields.Char("Name")
parent = fields.Many2One('test.model_child', "Parent")
class Singleton(ModelSingleton, ModelSQL):
'Singleton'
__name__ = 'test.singleton'
+28
View File
@@ -28,6 +28,34 @@ class ModelTestCase(unittest.TestCase):
self.assertEqual(
repr(record), "Pool().get('test.model')(%s)" % record.id)
@with_transaction()
def test_init_parent(self):
"Test __init__ with _parent_"
pool = Pool()
Model = pool.get('test.model_child')
values = {
'_parent_parent.name': "Test",
}
record = Model(**values)
self.assertEqual(record.parent.name, "Test")
@with_transaction()
def test_init_parent_parent(self):
"Test __init__ with _parent_._parent_"
pool = Pool()
Model = pool.get('test.model_child_child')
values = {
'_parent_parent.name': "Test 1",
'_parent_parent._parent_parent.name': "Test 2",
}
record = Model(**values)
self.assertEqual(record.parent.name, "Test 1")
self.assertEqual(record.parent.parent.name, "Test 2")
def suite():
return unittest.TestLoader().loadTestsFromTestCase(ModelTestCase)