Skip to content

Commit

Permalink
Merge pull request #536 from buczek/fix-full-clean
Browse files Browse the repository at this point in the history
Don't leak into current language from .clean_fields()
  • Loading branch information
last-partizan committed Jan 16, 2020
2 parents 426fa21 + 2c9669e commit 309397e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion modeltranslation/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,10 @@ def __set__(self, instance, value):
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.
# Setting the localized field may also be disabled by setting _mt_disable.
return
loc_field_name = build_localized_fieldname(self.field.name, get_language())
setattr(instance, loc_field_name, value)
Expand Down
6 changes: 5 additions & 1 deletion modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ def new_clean_fields(self, exclude=None):
if orig_field_name in exclude:
field.save_form_data(self, value, check=False)
delattr(self, '_mt_form_pending_clear')
old_clean_fields(self, exclude)
try:
setattr(self, '_mt_disable', True)
old_clean_fields(self, exclude)
finally:
setattr(self, '_mt_disable', False)
model.clean_fields = new_clean_fields


Expand Down

0 comments on commit 309397e

Please sign in to comment.