Skip to content

Commit 3afc80f

Browse files
author
Henry Walshaw
committed
Add the fix that filters out the excluded fields altogether
1 parent 79a895f commit 3afc80f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

pydantic_csv/basemodel_csv_writer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ def __init__(
4343
self._model = model
4444
self._field_mapping: dict[str, str] = {}
4545

46+
fields = {name: field for name, field in self._model.model_fields.items() if not (field.exclude or False)}
47+
4648
if use_alias:
47-
self._fieldnames = [field.alias or name for name, field in self._model.model_fields.items()]
49+
self._fieldnames = [field.alias or name for name, field in fields.items()]
4850
else:
49-
self._fieldnames = model.model_fields.keys()
51+
self._fieldnames = fields.keys()
5052

5153
self._writer = csv.writer(file_obj, dialect=dialect, **kwargs)
5254

0 commit comments

Comments
 (0)