diff options
Diffstat (limited to 'ishtar_common/model_merging.py')
-rw-r--r-- | ishtar_common/model_merging.py | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/ishtar_common/model_merging.py b/ishtar_common/model_merging.py index b2ed68358..be2867cb6 100644 --- a/ishtar_common/model_merging.py +++ b/ishtar_common/model_merging.py @@ -1,8 +1,19 @@ # from https://djangosnippets.org/snippets/2283/ +from django.apps import apps from django.db import transaction -from django.db.models import get_models, Model +from django.db.models import Model from django.contrib.contenttypes.fields import GenericForeignKey +from django.core.exceptions import ObjectDoesNotExist + + +def get_models(): + _apps = apps.app_configs.items() + models = [] + for app_name, app_config in _apps: + models += [apps.get_model(app_name, m) + for m in apps.get_app_config(app_name).models] + return models @transaction.atomic @@ -63,7 +74,10 @@ def merge_model_objects(primary_object, alias_objects=[], keep_old=False): alias_varname = related_object.get_accessor_name() # The variable name on the related model. obj_varname = related_object.field.name - related_objects = getattr(alias_object, alias_varname) + try: + related_objects = getattr(alias_object, alias_varname) + except ObjectDoesNotExist: + continue for obj in related_objects.all(): setattr(obj, obj_varname, primary_object) obj.save() |