From 94d01ba83908918b24229ae6f23b309033b69012 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Tue, 14 Jan 2020 15:14:45 +0100 Subject: [PATCH] Add patch to fix leaking translations Currently, when a translatable fields value is set via the admin intrface in a specific lungage (e.g. title_de) , that value leaks into the translated field of the language of the session (e.h. title_en). This patch tries to fix this. --- ...ons-Don-t-leak-into-current-language.patch | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 patches/modeltranslations-Don-t-leak-into-current-language.patch diff --git a/patches/modeltranslations-Don-t-leak-into-current-language.patch b/patches/modeltranslations-Don-t-leak-into-current-language.patch new file mode 100644 index 0000000..9270c83 --- /dev/null +++ b/patches/modeltranslations-Don-t-leak-into-current-language.patch @@ -0,0 +1,46 @@ +From 01e79ae2e0a78a7d0b2d6f9181babb6ac74c80c2 Mon Sep 17 00:00:00 2001 +From: Donald Buczek +Date: Mon, 13 Jan 2020 16:43:34 +0100 +Subject: [PATCH] modeltranslations: Don't leak into current language + +Disable the implicit setting of the translation field from +the translated field ( Rule 2 of [1] ) if we come from +clean_fields. See [2] + +[1] https://django-modeltranslation.readthedocs.io/en/latest/usage.html#rules-for-translated-field-access ) if we come from clean_fields +[2] https://github.com/deschler/django-modeltranslation/issues/382 +--- + lib/python3.7/site-packages/modeltranslation/fields.py | 2 +- + lib/python3.7/site-packages/modeltranslation/translator.py | 2 ++ + 2 files changed, 3 insertions(+), 1 deletion(-) + +diff --git a/lib/python3.7/site-packages/modeltranslation/fields.py b/lib/python3.7/site-packages/modeltranslation/fields.py +index d3c5a42..2ff0d83 100644 +--- a/lib/python3.7/site-packages/modeltranslation/fields.py ++++ b/lib/python3.7/site-packages/modeltranslation/fields.py +@@ -314,7 +314,7 @@ class TranslationFieldDescriptor(object): + instance.__dict__[self.field.name] = value + if isinstance(self.field, fields.related.ForeignKey): + instance.__dict__[self.field.get_attname()] = None if value is None else value.pk +- if getattr(instance, '_mt_init', False): ++ if getattr(instance, '_mt_init', False) or getattr(instance, '_mt_disable', False): + # When assignment takes place in model instance constructor, don't set value. + # This is essential for only/defer to work, but I think it's sensible anyway. + return +diff --git a/lib/python3.7/site-packages/modeltranslation/translator.py b/lib/python3.7/site-packages/modeltranslation/translator.py +index 901f701..65635d0 100644 +--- a/lib/python3.7/site-packages/modeltranslation/translator.py ++++ b/lib/python3.7/site-packages/modeltranslation/translator.py +@@ -267,7 +267,9 @@ def patch_clean_fields(model): + if orig_field_name in exclude: + field.save_form_data(self, value, check=False) + delattr(self, '_mt_form_pending_clear') ++ setattr(self, '_mt_disable', True) + old_clean_fields(self, exclude) ++ setattr(self, '_mt_disable', False) + model.clean_fields = new_clean_fields + + +-- +2.24.1 +