Ticket #12097: ModelChoiceField_doc.diff
File ModelChoiceField_doc.diff, 3.6 KB (added by , 15 years ago) |
---|
-
docs/ref/forms/fields.txt
751 751 Fields which handle relationships 752 752 --------------------------------- 753 753 754 For representing relationships between models, two fields are 755 provided which can derive their choices from a ``QuerySet``: 754 Two fields are available for representing relationships between 755 models: :class:`ModelChoiceField` and 756 :class:`ModelMultipleChoiceField`. Both of these fields require a 757 single ``queryset`` parameter that is used to create the choices for 758 the field. Upon form validation, these fields will place either one 759 model object (in the case of ``ModelChoiceField``) or multiple model 760 objects (in the case of ``ModelMultipleChoiceField``) into the 761 ``cleaned_data`` dictionary of the form. 756 762 763 ``ModelChoiceField`` 764 ~~~~~~~~~~~~~~~~~~~~ 765 757 766 .. class:: ModelChoiceField(**kwargs) 758 .. class:: ModelMultipleChoiceField(**kwargs)759 767 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: 768 Allows the selection of a single model object, suitable for 769 representing a foreign key. A single argument is required: 763 770 764 771 .. attribute:: ModelChoiceField.queryset 765 772 … … 767 774 field will be derived, and which will be used to validate the 768 775 user's selection. 769 776 770 ``ModelChoiceField`` 771 ~~~~~~~~~~~~~~~~~~~~ 777 ``ModelChoiceField`` also takes one optional argument: 772 778 773 Allows the selection of a single model object, suitable for774 representing a foreign key.775 776 The ``__unicode__`` method of the model will be called to generate777 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 model780 object, and should return a string suitable for representing it. For781 example::782 783 class MyModelChoiceField(ModelChoiceField):784 def label_from_instance(self, obj):785 return "My Object #%i" % obj.id786 787 779 .. attribute:: ModelChoiceField.empty_label 788 780 789 781 By default the ``<select>`` widget used by ``ModelChoiceField`` will have a … … 802 794 initial value, no empty choice is created (regardless of the value 803 795 of ``empty_label``). 804 796 797 The ``__unicode__`` method of the model will be called to generate 798 string representations of the objects for use in the field's choices; 799 to provide customized representations, subclass ``ModelChoiceField`` 800 and override ``label_from_instance``. This method will receive a model 801 object, and should return a string suitable for representing it. For 802 example:: 803 804 class MyModelChoiceField(ModelChoiceField): 805 def label_from_instance(self, obj): 806 return "My Object #%i" % obj.id 807 805 808 ``ModelMultipleChoiceField`` 806 809 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 807 810 811 .. class:: ModelMultipleChoiceField(**kwargs) 812 808 813 Allows the selection of one or more model objects, suitable for 809 representing a many-to-many relation. As with ``ModelChoiceField``,814 representing a many-to-many relation. As with :class:`ModelChoiceField`, 810 815 you can use ``label_from_instance`` to customize the object 811 representations .816 representations, and ``queryset`` is a required parameter: 812 817 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 813 824 Creating custom fields 814 825 ---------------------- 815 826