Fixed #18829 -- Fixed ModelChoiceIterator length
Thanks facundo.olano at gmail.com for the report and thikonom for the initial patch.
This commit is contained in:
parent
150e202172
commit
1c11ee6345
|
@ -917,7 +917,8 @@ class ModelChoiceIterator(object):
|
||||||
yield self.choice(obj)
|
yield self.choice(obj)
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self.queryset)
|
return len(self.queryset) +\
|
||||||
|
(1 if self.field.empty_label is not None else 0)
|
||||||
|
|
||||||
def choice(self, obj):
|
def choice(self, obj):
|
||||||
return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
|
return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
|
||||||
|
|
|
@ -989,6 +989,7 @@ class OldFormForXTests(TestCase):
|
||||||
(c2.pk, "It's a test"),
|
(c2.pk, "It's a test"),
|
||||||
(c3.pk, 'Third'),
|
(c3.pk, 'Third'),
|
||||||
(c4.pk, 'Fourth')])
|
(c4.pk, 'Fourth')])
|
||||||
|
self.assertEqual(5, len(f.choices))
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
f.clean('')
|
f.clean('')
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
|
|
Loading…
Reference in New Issue