Skip to content

Commit

Permalink
support copying of fields in ParentalManyToManyField
Browse files Browse the repository at this point in the history
  • Loading branch information
hpoul committed Sep 30, 2023
1 parent fd1a699 commit be6fe90
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
9 changes: 8 additions & 1 deletion wagtail_localize/fields.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion wagtail_localize/test/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions wagtail_localize/tests/test_translation_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit be6fe90

Please sign in to comment.