From 48f41dd690cbdc58f2bfa7f529b0b74e81184cc1 Mon Sep 17 00:00:00 2001 From: mle Date: Fri, 14 Apr 2023 09:28:06 +0200 Subject: [PATCH] [FIX] base_import_async: remove default values from context when creating the attachment. Some default values might be present in context depending on the action we came from when clicking on 'import' button. These default values are not intended to be default values for the ir.attachment record. In some cases they cause an error because a field with the same name exists on ir.attachment, as for e.g. the 'default_type'='opportunity' value present in the standard crm.lead action context. --- base_import_async/models/base_import_import.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/base_import_async/models/base_import_import.py b/base_import_async/models/base_import_import.py index de08c5efb1..dba862b531 100644 --- a/base_import_async/models/base_import_import.py +++ b/base_import_async/models/base_import_import.py @@ -88,9 +88,14 @@ def _create_csv_attachment(self, fields, data, options, file_name): writer.writerow(fields) for row in data: writer.writerow(row) - # create attachment + # create attachment. Remove default values from context + context = self.env.context + context_copy = {} + for key in context.keys(): + if not key.startswith("default_"): + context_copy[key] = context[key] datas = base64.encodebytes(f.getvalue().encode(encoding)) - attachment = self.env["ir.attachment"].create( + attachment = self.env["ir.attachment"].with_context(context_copy).create( {"name": file_name, "datas": datas} ) return attachment