From a2f7aba4e9aca9d6e9fc1f2ddc554daf87e8457e Mon Sep 17 00:00:00 2001 From: bryan brancotte Date: Sat, 27 Jan 2024 14:13:05 +0100 Subject: [PATCH] Preserve list values in CSV widget. --------- Co-authored-by: Cristian Torres --- django_filters/widgets.py | 4 ++++ tests/test_widgets.py | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/django_filters/widgets.py b/django_filters/widgets.py index fe99fec9..f5c99b11 100644 --- a/django_filters/widgets.py +++ b/django_filters/widgets.py @@ -205,6 +205,10 @@ def value_from_datadict(self, data, files, name): if value is not None: if value == "": # empty value should parse as an empty list return [] + if isinstance(value, list): + # since django.forms.widgets.SelectMultiple tries to use getlist + # if available, we should return value if it's already an array + return value return value.split(",") return None diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 9e3ba3bf..f50cc1be 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -383,6 +383,10 @@ class NumberCSVWidget(BaseCSVWidget, NumberInput): result = w.value_from_datadict(data, {}, "price") self.assertEqual(result, ["1", "2"]) + data = {"price": ["1", "2"]} + result = w.value_from_datadict(data, {}, "price") + self.assertEqual(result, ["1", "2"]) + data = {"price": "1,,2"} result = w.value_from_datadict(data, {}, "price") self.assertEqual(result, ["1", "", "2"])