summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKlaas van Schelven <klaas@vanschelven.com>2013-02-23 20:11:47 +0100
committerClaude Paroz <claude@2xlibre.net>2013-02-23 22:27:07 +0100
commit1c11ee63459c4362affbd6a20f2ddf9c2ecd8dce (patch)
treecde7c54aa2b650c0d8a1de54932a767bfca82d3a
parent150e202172034adb5175663411b15efacca52aae (diff)
Fixed #18829 -- Fixed ModelChoiceIterator length
Thanks facundo.olano at gmail.com for the report and thikonom for the initial patch.
-rw-r--r--django/forms/models.py3
-rw-r--r--tests/modeltests/model_forms/tests.py1
2 files changed, 3 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 75d526d173..3905e9e902 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -917,7 +917,8 @@ class ModelChoiceIterator(object):
yield self.choice(obj)
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):
return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index efaf2981e1..f9ef61536c 100644
--- a/tests/modeltests/model_forms/tests.py
+++ b/tests/modeltests/model_forms/tests.py
@@ -989,6 +989,7 @@ class OldFormForXTests(TestCase):
(c2.pk, "It's a test"),
(c3.pk, 'Third'),
(c4.pk, 'Fourth')])
+ self.assertEqual(5, len(f.choices))
with self.assertRaises(ValidationError):
f.clean('')
with self.assertRaises(ValidationError):