Ticket #12097: ModelChoiceField_doc.diff

File ModelChoiceField_doc.diff, 3.6 KB (added by Jason Yosinski, 15 years ago)
  • docs/ref/forms/fields.txt

     
    751751Fields which handle relationships
    752752---------------------------------
    753753
    754 For representing relationships between models, two fields are
    755 provided which can derive their choices from a ``QuerySet``:
     754Two fields are available for representing relationships between
     755models: :class:`ModelChoiceField` and
     756:class:`ModelMultipleChoiceField`.  Both of these fields require a
     757single ``queryset`` parameter that is used to create the choices for
     758the field.  Upon form validation, these fields will place either one
     759model object (in the case of ``ModelChoiceField``) or multiple model
     760objects (in the case of ``ModelMultipleChoiceField``) into the
     761``cleaned_data`` dictionary of the form.
    756762
     763``ModelChoiceField``
     764~~~~~~~~~~~~~~~~~~~~
     765
    757766.. class:: ModelChoiceField(**kwargs)
    758 .. class:: ModelMultipleChoiceField(**kwargs)
    759767
    760 These fields place one or more model objects into the ``cleaned_data``
    761 dictionary of forms in which they're used. Both of these fields have an
    762 additional required argument:
     768Allows the selection of a single model object, suitable for
     769representing a foreign key.  A single argument is required:
    763770
    764771.. attribute:: ModelChoiceField.queryset
    765772
     
    767774    field will be derived, and which will be used to validate the
    768775    user's selection.
    769776
    770 ``ModelChoiceField``
    771 ~~~~~~~~~~~~~~~~~~~~
     777``ModelChoiceField`` also takes one optional argument:
    772778
    773 Allows the selection of a single model object, suitable for
    774 representing a foreign key.
    775 
    776 The ``__unicode__`` method of the model will be called to generate
    777 string representations of the objects for use in the field's choices;
    778 to provide customized representations, subclass ``ModelChoiceField``
    779 and override ``label_from_instance``. This method will receive a model
    780 object, and should return a string suitable for representing it. For
    781 example::
    782 
    783     class MyModelChoiceField(ModelChoiceField):
    784         def label_from_instance(self, obj):
    785             return "My Object #%i" % obj.id
    786 
    787779.. attribute:: ModelChoiceField.empty_label
    788780
    789781   By default the ``<select>`` widget used by ``ModelChoiceField`` will have a
     
    802794   initial value, no empty choice is created (regardless of the value
    803795   of ``empty_label``).
    804796
     797The ``__unicode__`` method of the model will be called to generate
     798string representations of the objects for use in the field's choices;
     799to provide customized representations, subclass ``ModelChoiceField``
     800and override ``label_from_instance``. This method will receive a model
     801object, and should return a string suitable for representing it. For
     802example::
     803
     804    class MyModelChoiceField(ModelChoiceField):
     805        def label_from_instance(self, obj):
     806            return "My Object #%i" % obj.id
     807
    805808``ModelMultipleChoiceField``
    806809~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    807810
     811.. class:: ModelMultipleChoiceField(**kwargs)
     812
    808813Allows the selection of one or more model objects, suitable for
    809 representing a many-to-many relation. As with ``ModelChoiceField``,
     814representing a many-to-many relation. As with :class:`ModelChoiceField`,
    810815you can use ``label_from_instance`` to customize the object
    811 representations.
     816representations, and ``queryset`` is a required parameter:
    812817
     818.. attribute:: ModelMultipleChoiceField.queryset
     819
     820    A ``QuerySet`` of model objects from which the choices for the
     821    field will be derived, and which will be used to validate the
     822    user's selection.
     823
    813824Creating custom fields
    814825----------------------
    815826
Back to Top