From d8d14b33d387688a3d1b62e487ce96a8f6ef9c03 Mon Sep 17 00:00:00 2001 From: Jakub Musil Date: Thu, 15 Sep 2022 13:39:31 +0200 Subject: [PATCH 1/2] Fix for copying child relation model instances with inheritance models Issue: https://github.com/wagtail/wagtail/issues/8242 Docs: https://docs.djangoproject.com/en/4.1/topics/db/queries/#copying-model-instances --- modelcluster/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modelcluster/models.py b/modelcluster/models.py index bb586a9..f23ed5e 100644 --- a/modelcluster/models.py +++ b/modelcluster/models.py @@ -317,6 +317,8 @@ def copy_child_relation(self, child_relation, target, commit=False, append=False old_pk = child_object.pk is_saved = old_pk is not None child_object.pk = None + child_object.id = None + child_object._state.adding = True setattr(child_object, parental_key_name, target.id) target_manager.add(child_object) From c91fb766486125819fb05bfee98d68833e94de70 Mon Sep 17 00:00:00 2001 From: Jakub Musil Date: Sun, 6 Nov 2022 10:27:31 +0100 Subject: [PATCH 2/2] Update models.py --- modelcluster/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modelcluster/models.py b/modelcluster/models.py index f23ed5e..157a26a 100644 --- a/modelcluster/models.py +++ b/modelcluster/models.py @@ -316,6 +316,8 @@ def copy_child_relation(self, child_relation, target, commit=False, append=False for child_object in source_manager.all().order_by('pk'): old_pk = child_object.pk is_saved = old_pk is not None + if isinstance(child_object, ClusterableModel): + child_object, _ = child_object.copy_cluster() child_object.pk = None child_object.id = None child_object._state.adding = True