wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
226 stars 86 forks source link

Wrong error shown when trying to translate an object when a validation error occurs #669

Open maxbellec opened 1 year ago

maxbellec commented 1 year ago

First off, props for this great piece of software :pray:.

Imagine I want to translate an instance of this model Tag

class Tag(TagBase, TranslatableMixin):
    pass

Because it inherits from TagBase, it will have a slug field with a unique=True attribute. When translating from the admin, the TranslationSource is created fine, but the translated instance, another instance of Tag but with another locale, fails to be created because the database prevents saving another object with the same slug.

Because of this line of code : https://github.com/wagtail/wagtail-localize/blob/v1.3.3/wagtail_localize/operations.py#L81, the ValidationError is simply passed silently, and the error is hard to debug. The error shown is Tag matching query does not exist that occurs later because the translated instance does not exist. Full example traceback here.

Suggestions :

If it seems relevant for you, I'd be happy to try a PR to add the warning.

fdemmer commented 8 months ago

This issue still exists with wagtail-localize 1.7 and Wagtail version 5.2.3 and somewhat related to #10922.

In my case I am using a model to categorize blog posts in categories with a unique slug. Because of the unique field it is not possible to set up translations even after following these steps.

Setting exclude_fields_in_copy = ['slug'] does not help, as copy_synchronized_fields() is still called in create_or_update_translation().

Maybe a callback to "update" the value of unique fields should be implemented. It could append the language code by default.