fix: make doc.save work and empty tables

This commit is contained in:
Ankush Menat 2025-06-10 19:31:11 +05:30
parent eb77ddab69
commit 022dbf444d
2 changed files with 8 additions and 1 deletions

View file

@ -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

View file

@ -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"):