diff --git a/wagtail_localize/fields.py b/wagtail_localize/fields.py index 9df48589..9c1aa11d 100644 --- a/wagtail_localize/fields.py +++ b/wagtail_localize/fields.py @@ -1,5 +1,5 @@ from django.db import models -from modelcluster.fields import ParentalKey +from modelcluster.fields import ParentalKey, ParentalManyToManyField from modelcluster.models import ClusterableModel, get_all_child_relations from treebeard.mp_tree import MP_Node from wagtail.fields import RichTextField, StreamField @@ -285,6 +285,13 @@ def copy_synchronised_fields(source, target): else: source.copy_child_relation(field.name, target) + elif isinstance(field, ParentalManyToManyField): + parental_field = getattr(source, field.name) + if hasattr(parental_field, "all"): + values = parental_field.all() + if values: + setattr(target, field.attname, values) + else: # For all other fields, just set the attribute setattr(target, field.attname, getattr(source, field.attname)) diff --git a/wagtail_localize/test/models.py b/wagtail_localize/test/models.py index 00907c1f..1d6a322f 100644 --- a/wagtail_localize/test/models.py +++ b/wagtail_localize/test/models.py @@ -78,7 +78,7 @@ class TestUUIDSnippet(TranslatableMixin, models.Model): @register_snippet -class TestParentalSnippet(TranslatableMixin, models.Model): +class TestParentalSnippet(TranslatableMixin, ClusterableModel): field = ParentalManyToManyField( TestUUIDModel, blank=True, diff --git a/wagtail_localize/tests/test_translation_model.py b/wagtail_localize/tests/test_translation_model.py index ec4cfe26..5bcc07a8 100644 --- a/wagtail_localize/tests/test_translation_model.py +++ b/wagtail_localize/tests/test_translation_model.py @@ -726,13 +726,10 @@ def test_parental_many_to_many(self): target_locale=self.fr_locale, ) - # field_context = TranslationContext.objects.get(path="field") - # self.assertIsNotNone(field_context) - translation.save_target() translated_snippet = snippet.get_translation(self.fr_locale) - self.assertEqual(translated_snippet.field.charfield, "Some Test") + self.assertEqual(list(translated_snippet.field.all()), [foreign_key_target]) @override_settings(WAGTAILLOCALIZE_DISABLE_ON_DELETE=True)