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
This commit is contained in:
parent
6e030e7d1e
commit
d1e37f2b39
|
@ -777,15 +777,22 @@ for ``TimeField`` are used.
|
||||||
Fields which handle relationships
|
Fields which handle relationships
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
|
||||||
For representing relationships between models, two fields are
|
Two fields are available for representing relationships between
|
||||||
provided which can derive their choices from a ``QuerySet``:
|
models: :class:`ModelChoiceField` and
|
||||||
|
:class:`ModelMultipleChoiceField`. Both of these fields require a
|
||||||
|
single ``queryset`` parameter that is used to create the choices for
|
||||||
|
the field. Upon form validation, these fields will place either one
|
||||||
|
model object (in the case of ``ModelChoiceField``) or multiple model
|
||||||
|
objects (in the case of ``ModelMultipleChoiceField``) into the
|
||||||
|
``cleaned_data`` dictionary of the form.
|
||||||
|
|
||||||
|
``ModelChoiceField``
|
||||||
|
~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
.. class:: ModelChoiceField(**kwargs)
|
.. class:: ModelChoiceField(**kwargs)
|
||||||
.. class:: ModelMultipleChoiceField(**kwargs)
|
|
||||||
|
|
||||||
These fields place one or more model objects into the ``cleaned_data``
|
Allows the selection of a single model object, suitable for
|
||||||
dictionary of forms in which they're used. Both of these fields have an
|
representing a foreign key. A single argument is required:
|
||||||
additional required argument:
|
|
||||||
|
|
||||||
.. attribute:: ModelChoiceField.queryset
|
.. attribute:: ModelChoiceField.queryset
|
||||||
|
|
||||||
|
@ -793,22 +800,7 @@ additional required argument:
|
||||||
field will be derived, and which will be used to validate the
|
field will be derived, and which will be used to validate the
|
||||||
user's selection.
|
user's selection.
|
||||||
|
|
||||||
``ModelChoiceField``
|
``ModelChoiceField`` also takes one optional argument:
|
||||||
~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
Allows the selection of a single model object, suitable for
|
|
||||||
representing a foreign key.
|
|
||||||
|
|
||||||
The ``__unicode__`` method of the model will be called to generate
|
|
||||||
string representations of the objects for use in the field's choices;
|
|
||||||
to provide customized representations, subclass ``ModelChoiceField``
|
|
||||||
and override ``label_from_instance``. This method will receive a model
|
|
||||||
object, and should return a string suitable for representing it. For
|
|
||||||
example::
|
|
||||||
|
|
||||||
class MyModelChoiceField(ModelChoiceField):
|
|
||||||
def label_from_instance(self, obj):
|
|
||||||
return "My Object #%i" % obj.id
|
|
||||||
|
|
||||||
.. attribute:: ModelChoiceField.empty_label
|
.. attribute:: ModelChoiceField.empty_label
|
||||||
|
|
||||||
|
@ -828,13 +820,32 @@ example::
|
||||||
initial value, no empty choice is created (regardless of the value
|
initial value, no empty choice is created (regardless of the value
|
||||||
of ``empty_label``).
|
of ``empty_label``).
|
||||||
|
|
||||||
|
The ``__unicode__`` method of the model will be called to generate
|
||||||
|
string representations of the objects for use in the field's choices;
|
||||||
|
to provide customized representations, subclass ``ModelChoiceField``
|
||||||
|
and override ``label_from_instance``. This method will receive a model
|
||||||
|
object, and should return a string suitable for representing it. For
|
||||||
|
example::
|
||||||
|
|
||||||
|
class MyModelChoiceField(ModelChoiceField):
|
||||||
|
def label_from_instance(self, obj):
|
||||||
|
return "My Object #%i" % obj.id
|
||||||
|
|
||||||
``ModelMultipleChoiceField``
|
``ModelMultipleChoiceField``
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. class:: ModelMultipleChoiceField(**kwargs)
|
||||||
|
|
||||||
Allows the selection of one or more model objects, suitable for
|
Allows the selection of one or more model objects, suitable for
|
||||||
representing a many-to-many relation. As with ``ModelChoiceField``,
|
representing a many-to-many relation. As with :class:`ModelChoiceField`,
|
||||||
you can use ``label_from_instance`` to customize the object
|
you can use ``label_from_instance`` to customize the object
|
||||||
representations.
|
representations, and ``queryset`` is a required parameter:
|
||||||
|
|
||||||
|
.. attribute:: ModelMultipleChoiceField.queryset
|
||||||
|
|
||||||
|
A ``QuerySet`` of model objects from which the choices for the
|
||||||
|
field will be derived, and which will be used to validate the
|
||||||
|
user's selection.
|
||||||
|
|
||||||
Creating custom fields
|
Creating custom fields
|
||||||
----------------------
|
----------------------
|
||||||
|
|
Loading…
Reference in New Issue