Skip to content

Commit

Permalink
Add patch to fix leaking translations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
donald committed Jan 14, 2020
1 parent 1d2462f commit 94d01ba
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions patches/modeltranslations-Don-t-leak-into-current-language.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
From 01e79ae2e0a78a7d0b2d6f9181babb6ac74c80c2 Mon Sep 17 00:00:00 2001
From: Donald Buczek <buczek@molgen.mpg.de>
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

0 comments on commit 94d01ba

Please sign in to comment.