diff --git a/src/quorum/test/mock.py b/src/quorum/test/mock.py index 02502999..ad949454 100644 --- a/src/quorum/test/mock.py +++ b/src/quorum/test/mock.py @@ -61,6 +61,13 @@ class Person(quorum.Model): type = int ) + father = quorum.field( + type = quorum.reference( + "Person", + name = "identifier" + ) + ) + cats = quorum.field( type = quorum.references( "Cat", diff --git a/src/quorum/test/model.py b/src/quorum/test/model.py index e7432555..93cfe778 100644 --- a/src/quorum/test/model.py +++ b/src/quorum/test/model.py @@ -278,3 +278,25 @@ def test_references(self): self.assertEqual(isinstance(person, dict), True) self.assertEqual(isinstance(person["cats"], list), True) self.assertEqual(len(person["cats"]), 0) + + father = mock.Person() + father.name = "father" + father.save() + + person = mock.Person.get(identifier = 1) + person.father = father + person.save() + + person = mock.Person.get(identifier = 1) + + self.assertEqual(isinstance(person.father, quorum.Reference), True) + self.assertEqual(person.father.name, "father") + + person = mock.Person.get(identifier = 1) + + person.father.name = "father_changed" + person.father.save() + + person = mock.Person.get(identifier = 1) + + self.assertEqual(person.father.name, "father_changed") diff --git a/src/quorum/typesf.py b/src/quorum/typesf.py index 6731d303..dc0cf60c 100644 --- a/src/quorum/typesf.py +++ b/src/quorum/typesf.py @@ -364,6 +364,7 @@ def reference(target, name = None, eager = False): name = name or "id" target_t = type(target) is_reference = target_t in legacy.STRINGS + reserved = ("id", "_target", "_object", "_type", "__dict__") class _Reference(Reference): @@ -405,6 +406,11 @@ def __getattr__(self, name): raise AttributeError("'%s' not found" % name) def __setattr__(self, name, value): + # in case the name that is being set is not part of the reserved + # names for the reference underlying structure the object resolution + # is triggered to make sure the underlying object exists and is loaded + if not name in reserved: self.resolve() + # verifies if the reference object exists in the current # reference instance, that's the case if the object name is # defined in the dictionary and the referenced object contains