﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
29158	ModelChoiceField crashes when checking choices' length if queryset is a manager	François Freitag	François Freitag	"ModelChoiceField must accept Manager as a `.queryset` for backward compatibility reasons (see #25683). However, `ModelChoiceIterator` does not play nice with Managers:


{{{
class TestModelChoiceField(TestCase):
    def test_queryset_manager_has_length(self):
        f = ModelChoiceField(ChoiceOptionModel.objects)
        len(f.choices)
}}}

Errors with:

{{{
======================================================================
ERROR: test_queryset_manager_has_length (forms_tests.test_tmp.TestModelChoiceField)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ""django/tests/forms_tests/test_tmp.py"", line 10, in test_queryset_manager_has_length
    len(f.choices)
  File ""django/django/forms/models.py"", line 1141, in __len__
    return len(self.queryset) + (1 if self.field.empty_label is not None else 0)
TypeError: object of type 'Manager' has no len()
}}}

----

The reason why this `TypeError` was not reported earlier is probably because Python swallows `TypeError` silently for `__len__` when `list` is called, because generators have no `len`:

{{{
Python 3.6.4 (default, Jan  5 2018, 02:35:40) 
[GCC 7.2.1 20171224] on linux
Type ""help"", ""copyright"", ""credits"" or ""license"" for more information.
>>> def gene():
...     yield 1
... 
>>> len(gene())
Traceback (most recent call last):
  File ""<stdin>"", line 1, in <module>
TypeError: object of type 'generator' has no len()
>>> list(gene())
[1]"	Bug	closed	Forms	1.8	Normal	fixed		Gavin Wahl	Ready for checkin	1	0	0	0	0	0
