Skip to content

Commit

Permalink
strip whitespace
Browse files Browse the repository at this point in the history
django-import-export doesn't treat inputs like form fields in django, so
we must explicitly strip whitespace from input fields
  • Loading branch information
longhotsummer committed Aug 25, 2023
1 parent ba3b96f commit 1b3a51f
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion peachjam/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class ManyToOneWidget(ManyToManyWidget):
def clean(self, value, row=None, *args, **kwargs):
if not value:
return self.model.objects.none()
return [self.model(**{self.field: v}) for v in value.split(self.separator)]
values = [v.strip() for v in value.split(self.separator)]
return [self.model(**{self.field: v}) for v in values if v]


class StripHtmlWidget(CharWidget):
Expand Down Expand Up @@ -300,6 +301,9 @@ def download_attachment(url, document, nature):
return

def before_import_row(self, row, **kwargs):
# strip whitespace from all string fields
for k, v in row.items():
row[k] = v.strip() if isinstance(v, str) else v
if kwargs.get("user"):
row["created_by"] = kwargs["user"].id
if not row.get("skip"):
Expand Down

0 comments on commit 1b3a51f

Please sign in to comment.