Skip to content

Commit

Permalink
Preserve list values in CSV widget.
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Cristian Torres <cris.torres15@gmail.com>
  • Loading branch information
bryan-brancotte and tony13tv committed Jan 27, 2024
1 parent fd662c8 commit a2f7aba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions django_filters/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit a2f7aba

Please sign in to comment.