Refs #30998 -- Doc'd ModelChoiceField/ModelMultipleChoiceField.iterator attributes and ModelChoiceIterator.

This commit is contained in:
Jon Dufresne 2019-11-24 09:37:38 -08:00 committed by Mariusz Felisiak
parent 720de4d044
commit 5da85ea737
1 changed files with 40 additions and 0 deletions

View File

@ -1142,6 +1142,10 @@ method::
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields['foo_select'].queryset = ... self.fields['foo_select'].queryset = ...
Both ``ModelChoiceField`` and ``ModelMultipleChoiceField`` have an ``iterator``
attribute which specifies the class used to iterate over the queryset when
generating choices.
``ModelChoiceField`` ``ModelChoiceField``
-------------------- --------------------
@ -1222,6 +1226,13 @@ method::
... ...
</select> </select>
``ModelChoiceField`` also has the attribute:
.. attribute:: iterator
The iterator class used to generate field choices from ``queryset``. By
default, :class:`ModelChoiceIterator`.
The ``__str__()`` method of the model will be called to generate string The ``__str__()`` method of the model will be called to generate string
representations of the objects for use in the field's choices. To provide representations of the objects for use in the field's choices. To provide
customized representations, subclass ``ModelChoiceField`` and override customized representations, subclass ``ModelChoiceField`` and override
@ -1268,6 +1279,35 @@ method::
Same as :class:`ModelChoiceField.to_field_name`. Same as :class:`ModelChoiceField.to_field_name`.
``ModelMultipleChoiceField`` also has the attribute:
.. attribute:: iterator
Same as :class:`ModelChoiceField.iterator`.
``ModelChoiceIterator``
-----------------------
.. class:: ModelChoiceIterator(field)
The default class assigned to the ``iterator`` attribute of
:class:`ModelChoiceField` and :class:`ModelMultipleChoiceField`. An
iterable that yields 2-tuple choices from the queryset.
A single argument is required:
.. attribute:: field
The instance of ``ModelChoiceField`` or ``ModelMultipleChoiceField`` to
iterator and yield choice.
``ModelChoiceIterator`` has the following method:
.. method:: __iter__()
Yield 2-tuple choices in the same format as used by
:attr:`ChoiceField.choices`.
Creating custom fields Creating custom fields
====================== ======================