Merge pull request #668 from raghavsethi/strip

Strip whitespace when looking up ManyToMany fields
This commit is contained in:
Bojan Mihelac 2017-11-16 10:19:36 +01:00 committed by GitHub
commit 5af557423b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -90,3 +90,4 @@ The following is a list of much appreciated contributors:
* petrmifek
* ryan-copperleaf (Ryan OHara)
* gatsinski (Hristo Gatsinski)
* raghavsethi (Raghav Sethi)

View File

@ -376,7 +376,7 @@ class ManyToManyWidget(Widget):
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
})

View File

@ -209,6 +209,20 @@ class ManyToManyWidget(TestCase):
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)