Merge pull request #668 from raghavsethi/strip
Strip whitespace when looking up ManyToMany fields
This commit is contained in:
commit
5af557423b
1
AUTHORS
1
AUTHORS
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue