feat: Enable _doc_before_save for child documents (#33279)

* Initial Commit

* Apply suggestion from @ankush

* refactor: move unrelated code out of try-except block

* test: child table level value change detection

---------

Co-authored-by: Ankush Menat <ankushmenat@gmail.com>
Co-authored-by: Ankush Menat <ankush@frappe.io>
This commit is contained in:
P-Froggy 2025-07-11 14:33:00 +02:00 committed by GitHub
parent 2afc1582f6
commit 00a5fde3ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 1 deletions

View file

@ -1303,7 +1303,13 @@ class Document(BaseDocument):
if raise_exception:
raise
frappe.clear_last_message()
return frappe.clear_last_message()
for fieldname in self._table_fieldnames:
for row in self.get(fieldname):
row._doc_before_save = next(
(d for d in (self._doc_before_save.get(fieldname) or []) if d.name == row.name), None
)
def run_post_save_methods(self):
"""Run standard methods after `INSERT` or `UPDATE`. Standard Methods are:

View file

@ -143,6 +143,16 @@ class TestDocument(IntegrationTestCase):
self.assertFalse(d.has_value_changed("creation"))
self.assertFalse(d.has_value_changed("event_type"))
user = frappe.get_doc("User", "Administrator")
user.load_doc_before_save()
role1 = user.roles[0]
role2 = user.roles[1]
role1.role = "New Role"
self.assertTrue(role1.has_value_changed("role"))
self.assertFalse(role2.has_value_changed("role"))
def test_mandatory(self):
# TODO: recheck if it is OK to force delete
frappe.delete_doc_if_exists("User", "test_mandatory@example.com", 1)