mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 11:42:54 +03:00
Make defaults work for LocalizedIntegerField
This commit is contained in:
parent
fb233e8f25
commit
1b1d24a460
@ -32,9 +32,17 @@ class LocalizedIntegerField(LocalizedField):
|
|||||||
db_value = super().to_python(value)
|
db_value = super().to_python(value)
|
||||||
return self._convert_localized_value(db_value)
|
return self._convert_localized_value(db_value)
|
||||||
|
|
||||||
def get_prep_value(self, value: LocalizedValue) -> dict:
|
def get_prep_value(self, value: LocalizedIntegerValue) -> dict:
|
||||||
"""Gets the value in a format to store into the database."""
|
"""Gets the value in a format to store into the database."""
|
||||||
|
|
||||||
|
# apply default values
|
||||||
|
default_values = LocalizedIntegerValue(self.default)
|
||||||
|
if isinstance(value, LocalizedIntegerValue):
|
||||||
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
|
local_value = value.get(lang_code)
|
||||||
|
if local_value is None:
|
||||||
|
value.set(lang_code, default_values.get(lang_code, None))
|
||||||
|
|
||||||
prepped_value = super().get_prep_value(value)
|
prepped_value = super().get_prep_value(value)
|
||||||
if prepped_value is None:
|
if prepped_value is None:
|
||||||
return None
|
return None
|
||||||
@ -51,7 +59,7 @@ class LocalizedIntegerField(LocalizedField):
|
|||||||
|
|
||||||
# convert to a string before saving because the underlying
|
# convert to a string before saving because the underlying
|
||||||
# type is hstore, which only accept strings
|
# type is hstore, which only accept strings
|
||||||
prepped_value[lang_code] = str(local_value) if local_value else None
|
prepped_value[lang_code] = str(local_value) if local_value is not None else None
|
||||||
|
|
||||||
return prepped_value
|
return prepped_value
|
||||||
|
|
||||||
|
@ -163,3 +163,14 @@ class LocalizedIntegerFieldTestCase(TestCase):
|
|||||||
|
|
||||||
obj = model.objects.create()
|
obj = model.objects.create()
|
||||||
assert obj.score.get(settings.LANGUAGE_CODE) == 75
|
assert obj.score.get(settings.LANGUAGE_CODE) == 75
|
||||||
|
|
||||||
|
obj = model()
|
||||||
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
|
obj.score.set(lang_code, None)
|
||||||
|
obj.save()
|
||||||
|
|
||||||
|
for lang_code, _ in settings.LANGUAGES:
|
||||||
|
if lang_code == settings.LANGUAGE_CODE:
|
||||||
|
assert obj.score.get(lang_code) == 75
|
||||||
|
else:
|
||||||
|
assert obj.score.get(lang_code) is None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user