diff --git a/opal/models.py b/opal/models.py index 883e06a20..a74bad58a 100644 --- a/opal/models.py +++ b/opal/models.py @@ -200,11 +200,18 @@ def get_field_description(cls, name): @classmethod def get_field_enum(cls, name): field = cls._get_field(name) - choices = getattr(field, "choices", []) + choices = getattr(field, "choices", []) if choices: return [i[1] for i in choices] + yes_no = ["Yes", "No"] + if cls._get_field_type(name) == models.fields.BooleanField: + return yes_no + elif cls._get_field_type(name) == models.fields.NullBooleanField: + return yes_no + ["Unknown"] + + @classmethod def get_lookup_list_api_name(cls, field_name): lookup_list = None diff --git a/opal/tests/models.py b/opal/tests/models.py index 2358fbb69..459cb077f 100644 --- a/opal/tests/models.py +++ b/opal/tests/models.py @@ -37,6 +37,7 @@ class HatWearer(models.EpisodeSubrecord): name = dmodels.CharField(max_length=200) hats = dmodels.ManyToManyField(Hat, related_name="hat_wearers") wearing_a_hat = dmodels.BooleanField(default=True) + suits_a_hat = dmodels.NullBooleanField() class Meta: verbose_name = 'Wearer of Hats' diff --git a/opal/tests/test_templatetags_forms.py b/opal/tests/test_templatetags_forms.py index cd3e90ece..6d2d88f0e 100644 --- a/opal/tests/test_templatetags_forms.py +++ b/opal/tests/test_templatetags_forms.py @@ -449,6 +449,20 @@ def test_radio_infer_lookuplists(self): rendered = template.render(Context({})) self.assertIn('purple', rendered) + def test_radio_infer_lookuplists_with_boolean(self): + template = Template('{% load forms %}{% radio field="HatWearer.wearing_a_hat" %}') + rendered = template.render(Context({})) + self.assertIn('Yes', rendered) + self.assertIn('No', rendered) + self.assertNotIn('Unknown', rendered) + + def test_radio_infer_lookuplists_with_quantum_boolean(self): + template = Template('{% load forms %}{% radio field="HatWearer.suits_a_hat" %}') + rendered = template.render(Context({})) + self.assertIn('Yes', rendered) + self.assertIn('No', rendered) + self.assertIn('Unknown', rendered) + def test_element_name(self): tpl = Template('{% load forms %}{% radio label="hai" model="bai" element_name="onions"%}') rendered = tpl.render(Context({}))