Skip to content

Commit

Permalink
Merge pull request #78 from Rmvandiepen/patch-1
Browse files Browse the repository at this point in the history
Support nullable ListFields with django-rest-framework
  • Loading branch information
browniebroke authored Oct 11, 2024
2 parents 339e088 + 65e8874 commit aa02c51
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions drf_excel/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def __init__(self, list_sep, **kwargs):
super().__init__(**kwargs)

def prep_value(self) -> Any:
if self.value is None:
return super().prep_value()
if (
len(self.value) > 0
and isinstance(self.value[0], Iterable)
Expand Down
30 changes: 30 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,36 @@ def test_cell_complex_types(self, style: XLSXStyle, worksheet: Worksheet):
assert isinstance(cell, Cell)
assert cell.value == '[{"a": 1}, {"b": 2}]'

def test_cell_with_empty_list(self, style: XLSXStyle, worksheet: Worksheet):
f = XLSXListField(
list_sep=None,
key="objs",
value=[],
field=ListField(),
style=style,
mapping="",
cell_style=style,
)
assert f.original_value == f.value == []
cell = f.cell(worksheet, 1, 1)
assert isinstance(cell, Cell)
assert cell.value == ""

def test_cell_with_null_value(self, style: XLSXStyle, worksheet: Worksheet):
f = XLSXListField(
list_sep=None,
key="objs",
value=None,
field=ListField(allow_null=True),
style=style,
mapping="",
cell_style=style,
)
assert f.original_value is f.value is None
cell = f.cell(worksheet, 1, 1)
assert isinstance(cell, Cell)
assert cell.value is None


class TestXLSXBooleanField:
@pytest.mark.parametrize("value", [True, False])
Expand Down

0 comments on commit aa02c51

Please sign in to comment.