Skip to content

Commit f197adc

Browse files
committed
fix: Preserve blank value for optional fields
1 parent 1b0bf21 commit f197adc

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

modeltranslation/fields.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,22 @@ def __init__(
139139
# TODO: Do we really want to enforce null *at all*? Shouldn't this
140140
# better honour the null setting of the translated field?
141141
self.null = True
142+
143+
# We preserve original blank value for translated fields,
144+
# when they're in 'required_languages'.
145+
# So they will be only required if original field itself was required.
146+
original_blank = self.blank
142147
self.blank = True
143148

144-
# Take required_languages translation option into account
149+
# Take required_languages translation option into account.
145150
trans_opts = translator.get_options_for_model(self.model)
146151
if trans_opts.required_languages:
147152
required_languages = trans_opts.required_languages
148153
if isinstance(required_languages, (tuple, list)):
149154
# All fields
150155
if self.language in required_languages:
151156
# self.null = False
152-
self.blank = False
157+
self.blank = original_blank
153158
else:
154159
# Certain fields only
155160
# Try current language - if not present, try 'default' key
@@ -161,7 +166,7 @@ def __init__(
161166
# TODO: We might have to handle the whole thing through the
162167
# FieldsAggregationMetaClass, as fields can be inherited.
163168
# self.null = False
164-
self.blank = False
169+
self.blank = original_blank
165170

166171
# Adjust the name of this field to reflect the language
167172
self.attname = build_localized_fieldname(self.translated_field.name, language)

modeltranslation/tests/translation.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ class RequiredTranslationOptions(TranslationOptions):
282282
fields = ("non_req", "req", "req_reg", "req_en_reg")
283283
required_languages = {
284284
"en": (
285+
# We include `non_req` field here, to test that it's `blank` attribute is preserved,
286+
# even when languages is required.
287+
"non_req",
285288
"req_reg",
286289
"req_en_reg",
287290
),

0 commit comments

Comments
 (0)