diff --git a/README.rst b/README.rst index 577b359..fc1bed1 100644 --- a/README.rst +++ b/README.rst @@ -244,6 +244,16 @@ Besides ``LocalizedField``, there's also: title = LocalizedField() description = LocalizedBleachField() +Experimental feature +^^^^^^^^^^^^^^^^^^^^ + Enables the following experimental features: + * ``LocalizedField`` will return ``None`` instead of an empty ``LocalizedValue`` if there is no database value. + + .. code-block:: python + + LOCALIZED_FIELDS_EXPERIMENTAL = True + + Frequently asked questions (FAQ) -------------------------------- diff --git a/localized_fields/fields/localized_field.py b/localized_fields/fields/localized_field.py index 34ae876..6a3b909 100644 --- a/localized_fields/fields/localized_field.py +++ b/localized_fields/fields/localized_field.py @@ -1,8 +1,8 @@ from django.conf import settings from django.db.utils import IntegrityError +from psqlextra.fields import HStoreField from localized_fields import LocalizedFieldForm -from psqlextra.fields import HStoreField from ..localized_value import LocalizedValue @@ -36,11 +36,15 @@ class LocalizedField(HStoreField): """ if not value: - return LocalizedValue() + if getattr(settings, 'LOCALIZED_FIELDS_EXPERIMENTAL', False): + return None + else: + return LocalizedValue() return LocalizedValue(value) - def to_python(self, value: dict) -> LocalizedValue: + @staticmethod + def to_python(value: dict) -> LocalizedValue: """Turns the specified database value into its Python equivalent. diff --git a/localized_fields/models.py b/localized_fields/models.py index 2f681f8..d06d585 100644 --- a/localized_fields/models.py +++ b/localized_fields/models.py @@ -15,7 +15,7 @@ class LocalizedModel(PostgresModel): Here we set all the fields that are of :see:LocalizedField to an instance of :see:LocalizedValue in case they are none - so that the user doesn't explicitely have to do so.""" + so that the user doesn't explicitly have to do so.""" super(LocalizedModel, self).__init__(*args, **kwargs) diff --git a/settings.py b/settings.py index 99fc1c2..4cdade5 100644 --- a/settings.py +++ b/settings.py @@ -27,3 +27,5 @@ INSTALLED_APPS = ( # set to a lower number than the default, since # we want the tests to be fast, default is 100 LOCALIZED_FIELDS_MAX_RETRIES = 3 + +LOCALIZED_FIELDS_EXPERIMENTAL = False diff --git a/tests/test_localized_field.py b/tests/test_localized_field.py index f557422..ca92323 100644 --- a/tests/test_localized_field.py +++ b/tests/test_localized_field.py @@ -1,9 +1,9 @@ from django.conf import settings +from django.db.utils import IntegrityError from django.test import TestCase from django.utils import translation -from django.db.utils import IntegrityError -from localized_fields import LocalizedField, LocalizedValue, LocalizedFieldForm +from localized_fields import LocalizedField, LocalizedFieldForm, LocalizedValue def get_init_values() -> dict: @@ -179,7 +179,7 @@ class LocalizedFieldTestCase(TestCase): @staticmethod def test_from_db_value_none(): - """Tests whether the :see:from_db_valuei function + """Tests whether the :see:from_db_value function correctly handles None values.""" localized_value = LocalizedField.from_db_value(None) @@ -187,6 +187,18 @@ class LocalizedFieldTestCase(TestCase): for lang_code, _ in settings.LANGUAGES: assert localized_value.get(lang_code) is None + @staticmethod + def test_from_db_value_none_return_none(): + """Tests whether the :see:from_db_value function + correctly handles None values when LOCALIZED_FIELDS_EXPERIMENTAL + is set to True.""" + + settings.LOCALIZED_FIELDS_EXPERIMENTAL = True + localized_value = LocalizedField.from_db_value(None) + settings.LOCALIZED_FIELDS_EXPERIMENTAL = False + + assert localized_value is None + @staticmethod def test_to_python(): """Tests whether the :see:to_python function