diff --git a/constance/admin.py b/constance/admin.py index 8197af5..e36ef8b 100644 --- a/constance/admin.py +++ b/constance/admin.py @@ -89,9 +89,9 @@ class ConstanceForm(forms.Form): for name, options in settings.CONFIG.items(): default, help_text = options[0], options[1] - if len(options) == 3 and options[2] not in settings.ADDITIONAL_FIELDS: + if len(options) == 3: config_type = options[2] - if not isinstance(options[0], config_type): + if config_type not in settings.ADDITIONAL_FIELDS and not isinstance(options[0], config_type): raise ImproperlyConfigured(_("Default value type must be " "equal to declared config " "parameter type. Please fix " diff --git a/docs/index.rst b/docs/index.rst index d9efc71..048cba6 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -134,7 +134,7 @@ Note: Use later evaluated strings instead of direct classes for the field and wi CONSTANCE_ADDITIONAL_FIELDS = { 'yes_no_null_select': ['django.forms.fields.ChoiceField', { 'widget': 'django.forms.Select', - 'choices': (("-----", None), ("yes", "Yes"), ("no", "No")) + 'choices': ((None, "-----"), ("yes", "Yes"), ("no", "No")) }], } diff --git a/example/cheeseshop/settings.py b/example/cheeseshop/settings.py index 07d1d39..3091f5f 100644 --- a/example/cheeseshop/settings.py +++ b/example/cheeseshop/settings.py @@ -94,11 +94,24 @@ CONSTANCE_REDIS_CONNECTION = { 'db': 0, } +CONSTANCE_ADDITIONAL_FIELDS = { + 'yes_no_null_select': [ + 'django.forms.fields.ChoiceField', + { + 'widget': 'django.forms.Select', + 'choices': ((None, "-----"), ("yes", "Yes"), ("no", "No")) + } + ], + 'email': ('django.forms.fields.EmailField',), +} + CONSTANCE_CONFIG = { 'BANNER': ('The National Cheese Emporium', 'name of the shop'), 'OWNER': ('Mr. Henry Wensleydale', 'owner of the shop'), + 'OWNER_EMAIL': ('henry@example.com', 'contact email for owner', 'email'), 'MUSICIANS': (4, 'number of musicians inside the shop'), 'DATE_ESTABLISHED': (date(1972, 11, 30), "the shop's first opening"), + 'MY_SELECT_KEY': ('yes', 'select yes or no', 'yes_no_null_select'), } CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend' diff --git a/tests/settings.py b/tests/settings.py index 88d485d..fb65aab 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -52,7 +52,7 @@ CONSTANCE_ADDITIONAL_FIELDS = { 'django.forms.fields.ChoiceField', { 'widget': 'django.forms.Select', - 'choices': (("-----", None), ("yes", "Yes"), ("no", "No")) + 'choices': ((None, "-----"), ("yes", "Yes"), ("no", "No")) } ], # note this intentionally uses a tuple so that we can test immutable diff --git a/tests/test_form.py b/tests/test_form.py new file mode 100644 index 0000000..99208ae --- /dev/null +++ b/tests/test_form.py @@ -0,0 +1,25 @@ +from constance.admin import ConstanceForm +from django.forms import fields +from django.test import TestCase + + +class TestForm(TestCase): + + def test_form_field_types(self): + + f = ConstanceForm({}) + + self.assertIsInstance(f.fields['INT_VALUE'], fields.IntegerField) + self.assertIsInstance(f.fields['LONG_VALUE'], fields.IntegerField) + self.assertIsInstance(f.fields['BOOL_VALUE'], fields.BooleanField) + self.assertIsInstance(f.fields['STRING_VALUE'], fields.CharField) + self.assertIsInstance(f.fields['UNICODE_VALUE'], fields.CharField) + self.assertIsInstance(f.fields['DECIMAL_VALUE'], fields.DecimalField) + self.assertIsInstance(f.fields['DATETIME_VALUE'], fields.SplitDateTimeField) + self.assertIsInstance(f.fields['FLOAT_VALUE'], fields.FloatField) + self.assertIsInstance(f.fields['DATE_VALUE'], fields.DateField) + self.assertIsInstance(f.fields['TIME_VALUE'], fields.TimeField) + + # from CONSTANCE_ADDITIONAL_FIELDS + self.assertIsInstance(f.fields['CHOICE_VALUE'], fields.ChoiceField) + self.assertIsInstance(f.fields['EMAIL_VALUE'], fields.EmailField)