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

Strip whitespace when looking up ManyToMany fields #668

Merged
merged 1 commit into from
Nov 16, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ The following is a list of much appreciated contributors:
* petrmifek
* ryan-copperleaf (Ryan O’Hara)
* gatsinski (Hristo Gatsinski)
* raghavsethi (Raghav Sethi)
2 changes: 1 addition & 1 deletion import_export/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def clean(self, value, row=None, *args, **kwargs):
ids = [int(value)]
else:
ids = value.split(self.separator)
ids = filter(None, ids)
ids = filter(None, [i.strip() for i in ids])
return self.model.objects.filter(**{
'%s__in' % self.field: ids
})
Expand Down
14 changes: 14 additions & 0 deletions tests/core/tests/widgets_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,20 @@ def test_clean(self):
self.assertIn(self.cat1, cleaned_data)
self.assertIn(self.cat2, cleaned_data)

def test_clean_field(self):
value = "%s,%s" % (self.cat1.name, self.cat2.name)
cleaned_data = self.widget_name.clean(value)
self.assertEqual(len(cleaned_data), 2)
self.assertIn(self.cat1, cleaned_data)
self.assertIn(self.cat2, cleaned_data)

def test_clean_field_spaces(self):
value = "%s, %s" % (self.cat1.name, self.cat2.name)
cleaned_data = self.widget_name.clean(value)
self.assertEqual(len(cleaned_data), 2)
self.assertIn(self.cat1, cleaned_data)
self.assertIn(self.cat2, cleaned_data)

def test_clean_typo(self):
value = "%s," % self.cat1.pk
cleaned_data = self.widget.clean(value)
Expand Down