From f807212cf384f659c531c931f481b940cbaabc9d Mon Sep 17 00:00:00 2001 From: tudorvaran Date: Tue, 28 Apr 2020 18:31:15 +0300 Subject: [PATCH 1/4] Fix callable default usage in admin forms --- localized_fields/fields/field.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/localized_fields/fields/field.py b/localized_fields/fields/field.py index 1047f0e..9707ce6 100644 --- a/localized_fields/fields/field.py +++ b/localized_fields/fields/field.py @@ -11,6 +11,27 @@ from ..forms import LocalizedFieldForm from ..value import LocalizedValue +class FormClassFactory: + """When mixing callables as defaults and localized fields, Django will + render the default language value in the input form of the admin interface, + causing the printed hidden value to be diff-ed with the value provided in + the form, instead of a real instance of LocalizedField.""" + + def __init__(self, form_class): + self.form_class = form_class + + def produce(self, **kwargs): + """Remove the parameter which triggers the issue. + + Django should be able to determine the initial value without + printing it. That's how it worked before without any problems + """ + if kwargs.get("show_hidden_initial"): + del kwargs["show_hidden_initial"] + + return self.form_class(**kwargs) + + class LocalizedField(HStoreField): """A field that has the same value in multiple languages. @@ -220,7 +241,7 @@ class LocalizedField(HStoreField): """Gets the form field associated with this field.""" defaults = dict( - form_class=LocalizedFieldForm, + form_class=FormClassFactory(LocalizedFieldForm).produce, required=False if self.blank else self.required, ) defaults.update(kwargs) From 77e880787691039ad6cb60cf3d643dd48d582eae Mon Sep 17 00:00:00 2001 From: tudorvaran Date: Tue, 28 Apr 2020 21:16:15 +0300 Subject: [PATCH 2/4] Revert "Fix callable default usage in admin forms" This reverts commit f807212c --- localized_fields/fields/field.py | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/localized_fields/fields/field.py b/localized_fields/fields/field.py index 9707ce6..1047f0e 100644 --- a/localized_fields/fields/field.py +++ b/localized_fields/fields/field.py @@ -11,27 +11,6 @@ from ..forms import LocalizedFieldForm from ..value import LocalizedValue -class FormClassFactory: - """When mixing callables as defaults and localized fields, Django will - render the default language value in the input form of the admin interface, - causing the printed hidden value to be diff-ed with the value provided in - the form, instead of a real instance of LocalizedField.""" - - def __init__(self, form_class): - self.form_class = form_class - - def produce(self, **kwargs): - """Remove the parameter which triggers the issue. - - Django should be able to determine the initial value without - printing it. That's how it worked before without any problems - """ - if kwargs.get("show_hidden_initial"): - del kwargs["show_hidden_initial"] - - return self.form_class(**kwargs) - - class LocalizedField(HStoreField): """A field that has the same value in multiple languages. @@ -241,7 +220,7 @@ class LocalizedField(HStoreField): """Gets the form field associated with this field.""" defaults = dict( - form_class=FormClassFactory(LocalizedFieldForm).produce, + form_class=LocalizedFieldForm, required=False if self.blank else self.required, ) defaults.update(kwargs) From 3bf4435622b13ff946e0682f2aa723c59bc38473 Mon Sep 17 00:00:00 2001 From: tudorvaran Date: Tue, 28 Apr 2020 21:23:03 +0300 Subject: [PATCH 3/4] Disable show_hidden_initial for localized forms --- localized_fields/forms.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/localized_fields/forms.py b/localized_fields/forms.py index 314dab3..c1fe040 100644 --- a/localized_fields/forms.py +++ b/localized_fields/forms.py @@ -33,6 +33,10 @@ class LocalizedFieldForm(forms.MultiValueField): """Initializes a new instance of :see:LocalizedFieldForm.""" fields = [] + """ + Do not print initial value in html in the form of a hidden input. This will result in loss of information + """ + kwargs["show_hidden_initial"] = False for lang_code, _ in settings.LANGUAGES: field_options = dict( From 7f48903137fd02dd652b50e75440833b9166f11e Mon Sep 17 00:00:00 2001 From: tudorvaran Date: Wed, 29 Apr 2020 12:16:25 +0300 Subject: [PATCH 4/4] Change from docstring comment to normal --- localized_fields/forms.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/localized_fields/forms.py b/localized_fields/forms.py index c1fe040..32ebd35 100644 --- a/localized_fields/forms.py +++ b/localized_fields/forms.py @@ -33,9 +33,8 @@ class LocalizedFieldForm(forms.MultiValueField): """Initializes a new instance of :see:LocalizedFieldForm.""" fields = [] - """ - Do not print initial value in html in the form of a hidden input. This will result in loss of information - """ + + # Do not print initial value in html in the form of a hidden input. This will result in loss of information kwargs["show_hidden_initial"] = False for lang_code, _ in settings.LANGUAGES: