mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-03-16 22:40:26 +00:00
Fix for issue #648: Ensure choices are valid (value, label) tuples to avoid 'not enough values to unpack' error
(cherry picked from commit 30e8873b10db560b845f23697e00d20c42d7a989)
This commit is contained in:
parent
f6b3cf0865
commit
6e7aa119a8
2 changed files with 39 additions and 1 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue