Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nullable ListFields with django-rest-framework #78

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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