diff --git a/drf_excel/fields.py b/drf_excel/fields.py index f6b390b..62f0fb3 100644 --- a/drf_excel/fields.py +++ b/drf_excel/fields.py @@ -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) diff --git a/tests/test_fields.py b/tests/test_fields.py index 0789d00..112e09c 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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])