fix: better check of whether doctype is child table (#19329)

* fix: better check of whether doctype is child table

* perf: init `_table_fields` earlier
This commit is contained in:
Sagar Vora 2022-12-19 06:20:16 +00:00 committed by GitHub
parent 0d26627c66
commit 2fd2d426cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View file

@ -286,7 +286,7 @@ class BaseDocument:
return DOCTYPE_TABLE_FIELDS
# child tables don't have child tables
if self.doctype in DOCTYPES_FOR_DOCTYPE or getattr(self, "parentfield", None):
if self.doctype in DOCTYPES_FOR_DOCTYPE:
return ()
return self.meta.get_table_fields()

View file

@ -114,7 +114,7 @@ class Meta(Document):
# from cache
if isinstance(doctype, dict):
super().__init__(doctype)
self.init_field_map()
self.init_field_caches()
return
if isinstance(doctype, Document):
@ -137,12 +137,12 @@ class Meta(Document):
# don't process for special doctypes
# prevent's circular dependency
if self.name in self.special_doctypes:
self.init_field_map()
self.init_field_caches()
return
has_custom_fields = self.add_custom_fields()
self.apply_property_setters()
self.init_field_map()
self.init_field_caches()
if has_custom_fields:
self.sort_fields()
@ -214,12 +214,6 @@ class Meta(Document):
return self._set_only_once_fields
def get_table_fields(self):
if not hasattr(self, "_table_fields"):
if self.name != "DocType":
self._table_fields = self.get("fields", {"fieldtype": ["in", table_fields]})
else:
self._table_fields = DOCTYPE_TABLE_FIELDS
return self._table_fields
def get_global_search_fields(self):
@ -453,9 +447,16 @@ class Meta(Document):
self.set(fieldname, new_list)
def init_field_map(self):
def init_field_caches(self):
# field map
self._fields = {field.fieldname: field for field in self.fields}
# table fields
if self.name == "DocType":
self._table_fields = DOCTYPE_TABLE_FIELDS
else:
self._table_fields = self.get("fields", {"fieldtype": ["in", table_fields]})
def sort_fields(self):
"""Sort custom fields on the basis of insert_after"""