From cf8d9588e756790fcd64f4a1c8e24ff96b65ae9c Mon Sep 17 00:00:00 2001 From: Samar Hassan Date: Fri, 1 Nov 2024 08:51:46 +0000 Subject: [PATCH] fix :bug: field names can contains underscore as well --- oscar_odin/mappings/context.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/oscar_odin/mappings/context.py b/oscar_odin/mappings/context.py index f92adf3..3933d3f 100644 --- a/oscar_odin/mappings/context.py +++ b/oscar_odin/mappings/context.py @@ -274,9 +274,9 @@ def bulk_update_or_create_instances(self, instances): self.Model.objects.bulk_update(validated_instances_to_update, fields=fields) def bulk_update_or_create_one_to_many(self): - for relation, product, instances in self.get_all_o2m_instances: + for relation, parent, instances in self.get_all_o2m_instances: for instance in instances: - setattr(instance, relation.field.name, product) + setattr(instance, relation.field.name, parent) instances_to_create, instances_to_update, identities = self.get_o2m_relations @@ -315,14 +315,12 @@ def bulk_update_or_create_one_to_many(self): if fields is not None: conditions = Q() identifiers = self.identifier_mapping[relation.related_model] - for key in keys: + for key in set(keys): if isinstance(key, (list, tuple)): conditions |= Q(**dict(list(zip(identifiers, key)))) else: conditions |= Q(**{f"{identifiers[0]}": key}) - field_name = relation.remote_field.attname.replace( - "_", "__" - ).replace("id", instance_identifier) + field_name = f"{relation.remote_field.name}__{instance_identifier}" # Delete all related one_to_many instances where product is in the # given list of resources and excluding any instances present in # those resources