From 67ea35df52f2e29bafca8881e4f356934061644e Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 17 Nov 2019 15:41:23 -0800 Subject: Fixed #30998 -- Added ModelChoiceIteratorValue to pass the model instance to ChoiceWidget.create_option(). --- docs/ref/forms/fields.txt | 106 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 4 deletions(-) (limited to 'docs/ref/forms') diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index 0a2e13c85d..a74980a845 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -1144,7 +1144,7 @@ method:: Both ``ModelChoiceField`` and ``ModelMultipleChoiceField`` have an ``iterator`` attribute which specifies the class used to iterate over the queryset when -generating choices. +generating choices. See :ref:`iterating-relationship-choices` for details. ``ModelChoiceField`` -------------------- @@ -1285,8 +1285,73 @@ generating choices. Same as :class:`ModelChoiceField.iterator`. +.. _iterating-relationship-choices: + +Iterating relationship choices +------------------------------ + +By default, :class:`ModelChoiceField` and :class:`ModelMultipleChoiceField` use +:class:`ModelChoiceIterator` to generate their field ``choices``. + +When iterated, ``ModelChoiceIterator`` yields 2-tuple choices containing +:class:`ModelChoiceIteratorValue` instances as the first ``value`` element in +each choice. ``ModelChoiceIteratorValue`` wraps the choice value whilst +maintaining a reference to the source model instance that can be used in custom +widget implementations, for example, to add `data-* attributes`_ to +``