From 63eb23c8336cb78df9f2efe0f4d357abfca06dc4 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 25 Sep 2017 16:31:20 +0200 Subject: Merge: fix one to one field merge (such as person for ishtaruser) --- ishtar_common/model_merging.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'ishtar_common/model_merging.py') diff --git a/ishtar_common/model_merging.py b/ishtar_common/model_merging.py index a390f233c..3f392f596 100644 --- a/ishtar_common/model_merging.py +++ b/ishtar_common/model_merging.py @@ -22,7 +22,7 @@ def get_models(): @transaction.atomic def merge_model_objects(primary_object, alias_objects=[], keep_old=False): """ - Use this function to merge model objects (i.e. Users, Organizations, Polls, + Use this function to merge model objects (i.e. Users, Organizations, etc.) and migrate all of the related fields from the alias objects to the primary object. @@ -81,9 +81,14 @@ def merge_model_objects(primary_object, alias_objects=[], keep_old=False): related_objects = getattr(alias_object, alias_varname) except ObjectDoesNotExist: continue - for obj in related_objects.all(): - setattr(obj, obj_varname, primary_object) - obj.save() + if not hasattr(related_objects, 'all'): + # one to one field + setattr(related_objects, obj_varname, primary_object) + related_objects.save() + else: + for obj in related_objects.all(): + setattr(obj, obj_varname, primary_object) + obj.save() # Migrate all many to many references from alias object to primary # object. -- cgit v1.2.3