mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-04-11 17:00:58 +00:00
Fixed a bug in the admin integration where an emptied default language field (which has been defined with "blank=True") hasn't cleared the default field. Resolves issue 47.
This commit is contained in:
parent
5c0439c73c
commit
39262a8a02
2 changed files with 16 additions and 2 deletions
|
|
@ -8,7 +8,7 @@ from django.contrib.contenttypes import generic
|
|||
|
||||
from modeltranslation.settings import *
|
||||
from modeltranslation.translator import translator
|
||||
from modeltranslation.utils import get_translation_fields
|
||||
from modeltranslation.utils import get_translation_fields, build_localized_fieldname
|
||||
# Ensure that models are registered for translation before TranslationAdmin
|
||||
# runs. The import is supposed to resolve a race condition between model import
|
||||
# and translation registration in production (see issue 19).
|
||||
|
|
@ -112,6 +112,20 @@ class TranslationAdmin(admin.ModelAdmin, TranslationAdminBase):
|
|||
prepopulated_fields_new[k] = tuple([translation_fields[0]])
|
||||
self.prepopulated_fields = prepopulated_fields_new
|
||||
|
||||
def save_model(self, request, obj, form, change):
|
||||
# Rule is: 3. Assigning a value to a translation field of the default language also
|
||||
# updates the original field.
|
||||
# Ensure that an empty default language field value clears the default field.
|
||||
# See issue 47 for details.
|
||||
trans_opts = translator.get_options_for_model(self.model)
|
||||
for k, v in trans_opts.localized_fieldnames.items():
|
||||
if getattr(obj, k):
|
||||
default_lang_fieldname = build_localized_fieldname(k, DEFAULT_LANGUAGE)
|
||||
if not getattr(obj, default_lang_fieldname):
|
||||
# TODO: Handle null values
|
||||
setattr(obj, k, "")
|
||||
super(TranslationAdmin, self).save_model(request, obj, form, change)
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
# Call the baseclass function to get the formfield
|
||||
field = super(TranslationAdmin, self).formfield_for_dbfield(db_field,
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ def get_language():
|
|||
lang = lang.split('-')[0]
|
||||
if lang in AVAILABLE_LANGUAGES:
|
||||
return lang
|
||||
return AVAILABLE_LANGUAGES[0]
|
||||
return DEFAULT_LANGUAGE
|
||||
|
||||
|
||||
def get_translation_fields(field):
|
||||
|
|
|
|||
Loading…
Reference in a new issue