From 7503db4160d77f9075c92709bcaa5df8ec3ad44d Mon Sep 17 00:00:00 2001 From: Sagar Vora Date: Sat, 8 Mar 2025 12:12:43 +0530 Subject: [PATCH] perf: restrict doctypes to update when creating custom fields --- .../custom/doctype/custom_field/custom_field.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/frappe/custom/doctype/custom_field/custom_field.py b/frappe/custom/doctype/custom_field/custom_field.py index 413398b778..bac0d19a3e 100644 --- a/frappe/custom/doctype/custom_field/custom_field.py +++ b/frappe/custom/doctype/custom_field/custom_field.py @@ -333,7 +333,7 @@ def create_custom_fields(custom_fields: dict, ignore_validate=False, update=True doctypes = (doctypes,) for doctype in doctypes: - doctypes_to_update.add(doctype) + updated = False for df in fields: field = frappe.db.get_value("Custom Field", {"dt": doctype, "fieldname": df["fieldname"]}) @@ -342,15 +342,25 @@ def create_custom_fields(custom_fields: dict, ignore_validate=False, update=True df = df.copy() df["owner"] = "Administrator" create_custom_field(doctype, df, ignore_validate=ignore_validate) + updated = True except frappe.exceptions.DuplicateEntryError: pass elif update: custom_field = frappe.get_doc("Custom Field", field) - custom_field.flags.ignore_validate = ignore_validate + original_values = custom_field.__dict__.copy() custom_field.update(df) - custom_field.save() + + if original_values != custom_field.__dict__: + if ignore_validate: + custom_field.flags.ignore_validate = True + + custom_field.save() + updated = True + + if updated: + doctypes_to_update.add(doctype) for doctype in doctypes_to_update: frappe.clear_cache(doctype=doctype)