From 2b9b9d7aa79c54f62cef9932ef0c4424ccdc47bb Mon Sep 17 00:00:00 2001 From: wolfmetr Date: Mon, 23 Dec 2024 19:48:18 +0000 Subject: [PATCH] Fix for issue #648: Ensure choices are valid (value, label) tuples to avoid 'not enough values to unpack' error (cherry picked from commit 30e8873b10db560b845f23697e00d20c42d7a989) --- eav/forms.py | 2 +- tests/test_forms.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/eav/forms.py b/eav/forms.py index 5ff60a5..9595043 100644 --- a/eav/forms.py +++ b/eav/forms.py @@ -107,7 +107,7 @@ class BaseDynamicEntityForm(ModelForm): if datatype == attribute.TYPE_ENUM: values = attribute.get_choices().values_list("id", "value") - choices = ["", "-----", *list(values)] + choices = [("", ""), ("-----", "-----"), *list(values)] defaults.update({"choices": choices}) if value: diff --git a/tests/test_forms.py b/tests/test_forms.py index 38460a7..e1606e1 100644 --- a/tests/test_forms.py +++ b/tests/test_forms.py @@ -211,3 +211,41 @@ def test_entity_admin_form_no_attributes(patient): # 3 for 'name', 'email', 'example' assert total_fields == expected_fields + + +@pytest.mark.django_db +def test_dynamic_form_renders_enum_choices(): + """ + Test that enum choices render correctly in BaseDynamicEntityForm. + + This test verifies the fix for issue #648 where enum choices weren't + rendering correctly in Django 4.2.17 due to QuerySet unpacking issues. + """ + # Setup + eav.register(Patient) + + # Create enum values and group + female = EnumValue.objects.create(value="Female") + male = EnumValue.objects.create(value="Male") + gender_group = EnumGroup.objects.create(name="Gender") + gender_group.values.add(female, male) + + Attribute.objects.create( + name="gender", + datatype=Attribute.TYPE_ENUM, + enum_group=gender_group, + ) + + # Create a patient + patient = Patient.objects.create(name="Test Patient") + + # Initialize the dynamic form + form = PatientDynamicForm(instance=patient) + + # Test rendering - should not raise any exceptions + rendered_form = form.as_p() + + # Verify the form rendered and contains the enum choices + assert 'name="gender"' in rendered_form + assert f'value="{female.pk}">{female.value}' in rendered_form + assert f'value="{male.pk}">{male.value}' in rendered_form