diff --git a/frappe/model/naming.py b/frappe/model/naming.py index f6a3846699..b674b0cd81 100644 --- a/frappe/model/naming.py +++ b/frappe/model/naming.py @@ -317,9 +317,9 @@ def parse_naming_series( part = frappe.defaults.get_user_default("fiscal_year") elif e.startswith("{") and doc: e = e.replace("{", "").replace("}", "") - part = doc.get(e) + part = (cstr(doc.get(e)) or "").strip() elif doc and doc.get(e): - part = doc.get(e) + part = (cstr(doc.get(e)) or "").strip() else: part = e diff --git a/frappe/tests/test_naming.py b/frappe/tests/test_naming.py index d966fd5ce8..305df66209 100644 --- a/frappe/tests/test_naming.py +++ b/frappe/tests/test_naming.py @@ -12,7 +12,7 @@ from frappe.model.naming import ( revert_series_if_last, ) from frappe.tests.utils import FrappeTestCase -from frappe.utils import now_datetime +from frappe.utils import now_datetime, nowdate, nowtime class TestNaming(FrappeTestCase): @@ -100,6 +100,20 @@ class TestNaming(FrappeTestCase): self.assertEqual(doc.name, f"TODO-{now_datetime().strftime('%m')}-{description}-{series:02}") + def test_format_autoname_for_datetime_field(self): + """Test if datetime, date and time objects get converted to strings for naming.""" + doctype = new_doctype(autoname="format:TODO-{field}-{##}").insert() + + for field in [now_datetime(), nowdate(), nowtime()]: + doc = frappe.new_doc(doctype.name) + doc.field = field + doc.insert() + + series = getseries("", 2) + series = int(series) - 1 + + self.assertEqual(doc.name, f"TODO-{field}-{series:02}") + def test_format_autoname_for_consecutive_week_number(self): """ Test if braced params are replaced for consecutive week number in format autoname