Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SynchronizedField for a ParentalManyToManyField leads to TypeError: 'DeferringManyRelatedManager' object is not iterable #563

Closed
hpoul opened this issue May 3, 2022 · 0 comments · Fixed by #564

Comments

@hpoul
Copy link
Contributor

hpoul commented May 3, 2022

Hello,
I have a translatable snippet with a ParentalManyToManyField to another model and want it to be synchronized. But when trying to translate this page I get the following exception:

Traceback (most recent call last):
  File "/Users/herbert/dev/python/wagtail-localize/wagtail_localize/tests/test_translation_model.py", line 721, in test_parental_many_to_many
    translation.save_target()
  File "/Users/herbert/dev/python/wagtail-localize/wagtail_localize/models.py", line 1340, in save_target
    self.source.create_or_update_translation(
  File "/Users/herbert/dev/python/wagtail-localize/wagtail_localize/models.py", line 762, in create_or_update_translation
    copy_synchronised_fields(original, translation)
  File "/Users/herbert/dev/python/wagtail-localize/wagtail_localize/fields.py", line 296, in copy_synchronised_fields
    setattr(target, field.attname, getattr(source, field.attname))
  File "/Users/herbert/dev/python/wagtail-localize/venv/lib/python3.9/site-packages/modelcluster/fields.py", line 490, in __set__
    manager.set(value)
  File "/Users/herbert/dev/python/wagtail-localize/venv/lib/python3.9/site-packages/modelcluster/fields.py", line 424, in set
    objs = list(objs)
TypeError: 'DeferringManyRelatedManager' object is not iterable

I have created a quick repro test here:

https://github.com/hpoul/wagtail-localize/blob/64766882587d02b64822e903a21842b3c829e38f/wagtail_localize/test/models.py#L61-L68

and

https://github.com/hpoul/wagtail-localize/blob/64766882587d02b64822e903a21842b3c829e38f/wagtail_localize/tests/test_translation_model.py#L708-L725

@register_snippet
class TestParentalSnippet(TranslatableMixin, models.Model):
    field = ParentalManyToManyField(
        TestUUIDModel,
        blank=True,
    )

    translatable_fields = [SynchronizedField("field")]

and

    def test_parental_many_to_many(self):
        foreign_key_target = TestUUIDModel.objects.create(charfield="Some Test")
        snippet = TestParentalSnippet.objects.create()
        snippet.field = [foreign_key_target]

        source, created = TranslationSource.get_or_create_from_instance(snippet)
        translation = Translation.objects.create(
            source=source,
            target_locale=self.fr_locale,
        )

        translation.save_target()

Am I doing something wrong? Should ParentalManyToManyField be supported to be a synchronized field?

thanks,
Herbert

hpoul added a commit to hpoul/wagtail-localize that referenced this issue Sep 30, 2023
…#563

regenerate migrations (+4 squashed commits)
Squashed commits:
[d65cc37] remove unnecessary migration dependency.
[be6fe90] support copying of fields in ParentalManyToManyField
[fd1a699] failing test for ParentalManyToManyField
[48b512c] testmanage.py makemigrations --merge
hpoul added a commit to hpoul/wagtail-localize that referenced this issue Sep 30, 2023
…#563

regenerate migrations (+4 squashed commits)
Squashed commits:
[d65cc37] remove unnecessary migration dependency.
[be6fe90] support copying of fields in ParentalManyToManyField
[fd1a699] failing test for ParentalManyToManyField
[48b512c] testmanage.py makemigrations --merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant