Browse Source

fix: Calculate dynamic properties for as_dict

If you have a @property in the controller class, this data used to not
be accessible anywhere other than directly through the object via
`frappe.get_doc`. This fix changes that.
version-14
Gavin D'souza 3 years ago
parent
commit
d9daefc54d
1 changed files with 6 additions and 0 deletions
  1. +6
    -0
      frappe/model/base_document.py

+ 6
- 0
frappe/model/base_document.py View File

@@ -264,6 +264,11 @@ class BaseDocument(object):
if isinstance(d[fieldname], list) and df.fieldtype not in table_fields: if isinstance(d[fieldname], list) and df.fieldtype not in table_fields:
frappe.throw(_('Value for {0} cannot be a list').format(_(df.label))) frappe.throw(_('Value for {0} cannot be a list').format(_(df.label)))


if d[fieldname] == None:
_val = getattr(self, fieldname, None)
if not callable(_val):
d[fieldname] = _val

if convert_dates_to_str and isinstance(d[fieldname], ( if convert_dates_to_str and isinstance(d[fieldname], (
datetime.datetime, datetime.datetime,
datetime.date, datetime.date,
@@ -307,6 +312,7 @@ class BaseDocument(object):
def as_dict(self, no_nulls=False, no_default_fields=False, convert_dates_to_str=False): def as_dict(self, no_nulls=False, no_default_fields=False, convert_dates_to_str=False):
doc = self.get_valid_dict(convert_dates_to_str=convert_dates_to_str) doc = self.get_valid_dict(convert_dates_to_str=convert_dates_to_str)
doc["doctype"] = self.doctype doc["doctype"] = self.doctype

for df in self.meta.get_table_fields(): for df in self.meta.get_table_fields():
children = self.get(df.fieldname) or [] children = self.get(df.fieldname) or []
doc[df.fieldname] = [d.as_dict(convert_dates_to_str=convert_dates_to_str, no_nulls=no_nulls, no_default_fields=no_default_fields) for d in children] doc[df.fieldname] = [d.as_dict(convert_dates_to_str=convert_dates_to_str, no_nulls=no_nulls, no_default_fields=no_default_fields) for d in children]


Loading…
Cancel
Save