diff --git a/frappe/model/document.py b/frappe/model/document.py index a6b8ca3a16..be0040c7ab 100644 --- a/frappe/model/document.py +++ b/frappe/model/document.py @@ -1983,7 +1983,7 @@ class LazyChildTable: self.fieldname = fieldname self.doctype = doctype - def __get__(self, doc, objtype=None): + def __get__(self, doc: Document, objtype=None): # TODO: review cached_property magic children = frappe.db.sql( """SELECT * FROM {table_name} @@ -1998,5 +1998,6 @@ class LazyChildTable: ) # Update __dict__ and convert to Document objects + doc.__dict__[self.fieldname] = [] doc.extend(self.fieldname, children or []) return doc.__dict__[self.fieldname] # Note: avoid any high level access here diff --git a/frappe/tests/test_document.py b/frappe/tests/test_document.py index 890af417b6..87dc00efba 100644 --- a/frappe/tests/test_document.py +++ b/frappe/tests/test_document.py @@ -541,6 +541,10 @@ class TestDocument(IntegrationTestCase): self.assertEqual(guest_role.role, "Guest") self.assertIsInstance(guest_role, type(eager_guest.roles[0])) + # Only one query for one table access + with self.assertQueryCount(1): + _ = guest.role_profiles + # No queries for repeat access, same object with self.assertQueryCount(0): guest_role_repeat_access = guest.roles[0] @@ -552,6 +556,8 @@ class TestDocument(IntegrationTestCase): # things accessing __dict__ by default should be updated too self.assertTrue(frappe.get_lazy_doc("User", "Guest").get("roles")) + guest.save() + class TestDocumentWebView(IntegrationTestCase): def get(self, path, user="Guest"):