feat: Remove null and empty attributes from DocType JSON file on export

This commit is contained in:
Saif Ur Rehman 2019-04-18 17:52:47 +05:00
parent fc410ee950
commit a496e5b91c
2 changed files with 17 additions and 1 deletions

View file

@ -18,6 +18,7 @@ from frappe.modules import make_boilerplate, get_doc_path
from frappe.database.schema import validate_column_name, validate_column_length
from frappe.model.docfield import supports_translation
from frappe.modules.import_file import get_file_path
from six import iteritems
import frappe.website.render
import json
@ -407,6 +408,21 @@ class DocType(Document):
make_property_setter(self.name, "naming_series", "default", naming_series[0].default, "Text", validate_fields_for_doctype=False)
def before_export(self, docdict):
# remove null and empty fields
def remove_null_fields(o):
to_remove = []
for attr, value in iteritems(o):
if isinstance(value, list):
for v in value:
remove_null_fields(v)
elif not value:
to_remove.append(attr)
for attr in to_remove:
del o[attr]
remove_null_fields(docdict)
# retain order of 'fields' table and change order in 'field_order'
docdict["field_order"] = [f.fieldname for f in self.fields]

View file

@ -4,7 +4,7 @@
from __future__ import unicode_literals, print_function
import frappe, os, json
from frappe.modules import get_module_path, scrub_dt_dn, load_doctype_module
from frappe.modules import get_module_path, scrub_dt_dn
from frappe.utils import get_datetime_str
from frappe.model.base_document import get_controller