When we create a Variant from a template then all the fields from template are copied to the variant (only if the field is not set to no_copy
).
Now I would like to better this logic and I am trying to copy all the values from the template even when a variant is updated. This would ensure that the values in the template and its variant are all same at all times without a bypass.
I am trying to accomplish the same via this code:
def validate_variants(doc,method):
if doc.variant_of:
#Check if all variants are mentioned in the Item Variant Table as per the Template.
template = frappe.get_doc("Item", doc.variant_of)
template_attribute = []
variant_attribute = []
template_restricted_attributes = {}
template_rest_summary = []
copy_template_fields(template, doc)
def copy_template_fields(item, doc):
from frappe.model import no_value_fields
for field in item.meta.fields:
if field.fieldtype not in no_value_fields and (not field.no_copy)\
and field.fieldname not in ("item_code", "item_name", "show_in_website",\
"variant_of", "description", "web_long_description"):
if doc.get(field.fieldname) != item.get(field.fieldname):
change_field = field.fieldname
doc.change_field = item.get(field.fieldname)
doc.variant_of = item.name
doc.has_variants = 0
Now this code is working fine since it returns correct values but it is unable to copy the values of the fields from the template.
Can any one help me with this problem.