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

Expand valid values for BooleanWidget #1071

Merged
merged 5 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Changelog
2.0.2 (unreleased)
------------------

- Nothing changed yet.

- Deal with importing a BooleanField that actually has `True`, `False`, and
`None` values. (#1071)

2.0.1 (2020-01-15)
------------------
Expand Down
11 changes: 6 additions & 5 deletions import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ class BooleanWidget(Widget):
"""
Widget for converting boolean fields.
"""
TRUE_VALUES = ["1", 1]
FALSE_VALUE = "0"
TRUE_VALUES = ["1", 1, True, "true", "TRUE", "True"]
FALSE_VALUES = ["0", False, "false", "FALSE", "False"]
NULL_VALUES = ["", None, "null", "NULL", "none", "NONE", "None"]

def render(self, value, obj=None):
if value is None:
if value in self.NULL_VALUES:
return ""
return self.TRUE_VALUES[0] if value else self.FALSE_VALUE
return self.TRUE_VALUES[0] if value else self.FALSE_VALUES[0]

def clean(self, value, row=None, *args, **kwargs):
if value == "":
if value in self.NULL_VALUES:
return None
return True if value in self.TRUE_VALUES else False

Expand Down