Skip to content

Commit d1e37f2

Browse files
committed
Fixed #12097 -- Cleaned up the documentation for ModelChoiceField and MultipleModelChoiceField. Thanks to JasonYosinski for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12712 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 6e030e7 commit d1e37f2

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

docs/ref/forms/fields.txt

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -777,38 +777,30 @@ for ``TimeField`` are used.
777777
Fields which handle relationships
778778
---------------------------------
779779

780-
For representing relationships between models, two fields are
781-
provided which can derive their choices from a ``QuerySet``:
780+
Two fields are available for representing relationships between
781+
models: :class:`ModelChoiceField` and
782+
:class:`ModelMultipleChoiceField`. Both of these fields require a
783+
single ``queryset`` parameter that is used to create the choices for
784+
the field. Upon form validation, these fields will place either one
785+
model object (in the case of ``ModelChoiceField``) or multiple model
786+
objects (in the case of ``ModelMultipleChoiceField``) into the
787+
``cleaned_data`` dictionary of the form.
788+
789+
``ModelChoiceField``
790+
~~~~~~~~~~~~~~~~~~~~
782791

783792
.. class:: ModelChoiceField(**kwargs)
784-
.. class:: ModelMultipleChoiceField(**kwargs)
785793

786-
These fields place one or more model objects into the ``cleaned_data``
787-
dictionary of forms in which they're used. Both of these fields have an
788-
additional required argument:
794+
Allows the selection of a single model object, suitable for
795+
representing a foreign key. A single argument is required:
789796

790797
.. attribute:: ModelChoiceField.queryset
791798

792799
A ``QuerySet`` of model objects from which the choices for the
793800
field will be derived, and which will be used to validate the
794801
user's selection.
795802

796-
``ModelChoiceField``
797-
~~~~~~~~~~~~~~~~~~~~
798-
799-
Allows the selection of a single model object, suitable for
800-
representing a foreign key.
801-
802-
The ``__unicode__`` method of the model will be called to generate
803-
string representations of the objects for use in the field's choices;
804-
to provide customized representations, subclass ``ModelChoiceField``
805-
and override ``label_from_instance``. This method will receive a model
806-
object, and should return a string suitable for representing it. For
807-
example::
808-
809-
class MyModelChoiceField(ModelChoiceField):
810-
def label_from_instance(self, obj):
811-
return "My Object #%i" % obj.id
803+
``ModelChoiceField`` also takes one optional argument:
812804

813805
.. attribute:: ModelChoiceField.empty_label
814806

@@ -828,13 +820,32 @@ example::
828820
initial value, no empty choice is created (regardless of the value
829821
of ``empty_label``).
830822

823+
The ``__unicode__`` method of the model will be called to generate
824+
string representations of the objects for use in the field's choices;
825+
to provide customized representations, subclass ``ModelChoiceField``
826+
and override ``label_from_instance``. This method will receive a model
827+
object, and should return a string suitable for representing it. For
828+
example::
829+
830+
class MyModelChoiceField(ModelChoiceField):
831+
def label_from_instance(self, obj):
832+
return "My Object #%i" % obj.id
833+
831834
``ModelMultipleChoiceField``
832835
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
833836

837+
.. class:: ModelMultipleChoiceField(**kwargs)
838+
834839
Allows the selection of one or more model objects, suitable for
835-
representing a many-to-many relation. As with ``ModelChoiceField``,
840+
representing a many-to-many relation. As with :class:`ModelChoiceField`,
836841
you can use ``label_from_instance`` to customize the object
837-
representations.
842+
representations, and ``queryset`` is a required parameter:
843+
844+
.. attribute:: ModelMultipleChoiceField.queryset
845+
846+
A ``QuerySet`` of model objects from which the choices for the
847+
field will be derived, and which will be used to validate the
848+
user's selection.
838849

839850
Creating custom fields
840851
----------------------

0 commit comments

Comments
 (0)