Ticket #10239: 10239.diff
File 10239.diff, 7.3 KB (added by , 12 years ago) |
---|
-
django/forms/models.py
diff --git a/django/forms/models.py b/django/forms/models.py index e9b71cc..af78f0e 100644
a b def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c 141 141 ``exclude`` is an optional list of field names. If provided, the named 142 142 fields will be excluded from the returned fields, even if they are listed 143 143 in the ``fields`` argument. 144 145 ``widgets`` is a dictionary of model field names mapped to a widget 146 147 ``formfield_callback`` is a callable that takes a model field and returns 148 a form field. 144 149 """ 145 150 field_list = [] 146 151 ignored = [] … … class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)): 371 376 372 377 def modelform_factory(model, form=ModelForm, fields=None, exclude=None, 373 378 formfield_callback=None, widgets=None): 379 """ 380 Returns a ModelForm containing form fields for the given model. 381 382 ``fields`` is an optional list of field names. If provided, only the named 383 fields will be included in the returned fields. 384 385 ``exclude`` is an optional list of field names. If provided, the named 386 fields will be excluded from the returned fields, even if they are listed 387 in the ``fields`` argument. 388 389 ``widgets`` is a dictionary of model field names mapped to a widget. 390 391 ``formfield_callback`` is a callable that takes a model field and returns 392 a form field. 393 """ 374 394 # Create the inner Meta class. FIXME: ideally, we should be able to 375 395 # construct a ModelForm without creating and passing in a temporary 376 396 # inner class. -
docs/ref/forms/index.txt
diff --git a/docs/ref/forms/index.txt b/docs/ref/forms/index.txt index 866afed..446fdb8 100644
a b Detailed form API reference. For introductory material, see :doc:`/topics/forms/ 9 9 10 10 api 11 11 fields 12 models 12 13 widgets 13 14 validation -
new file docs/ref/forms/models.txt
diff --git a/docs/ref/forms/models.txt b/docs/ref/forms/models.txt new file mode 100644 index 0000000..1f4a0d0
- + 1 ==================== 2 Model Form Functions 3 ==================== 4 5 .. module:: django.forms.models 6 :synopsis: Django's functions for building model forms and formsets. 7 8 .. method:: modelform_factory(model, form=ModelForm, fields=None, exclude=None, formfield_callback=None, widgets=None) 9 10 Returns a :class:`~django.forms.ModelForm` class for the given ``model``. 11 You can optionally pass a ``form`` argument to use as a starting point for 12 constructing the ``ModelForm``. 13 14 ``fields`` is an optional list of field names. If provided, only the named 15 fields will be included in the returned fields. 16 17 ``exclude`` is an optional list of field names. If provided, the named 18 fields will be excluded from the returned fields, even if they are listed 19 in the ``fields`` argument. 20 21 ``widgets`` is a dictionary of model field names mapped to a widget. 22 23 ``formfield_callback`` is a callable that takes a model field and returns 24 a form field. 25 26 See :ref:`modelforms-factory` for example usage. 27 28 .. method:: modelformset_factory(model, form=ModelForm, formfield_callback=None, formset=BaseModelFormSet, extra=1, can_delete=False, can_order=False, max_num=None, fields=None, exclude=None) 29 30 Returns a ``FormSet`` class for the given ``model`` class. 31 32 Arguments ``model``, ``form``, ``fields``, ``exclude``, and 33 ``formfield_callback`` are all passed through to 34 :meth:`~django.forms.models.modelform_factory`. 35 36 Arguments ``formset``, ``extra``, ``max_num``, ``can_order``, and 37 ``can_delete`` are passed through to ``formset_factory``. See 38 :ref:`formsets` for details. 39 40 See :ref:`model-formsets` for example usage. -
docs/topics/forms/modelforms.txt
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index fcc9bd6..9c97b34 100644
a b for more on how field cleaning and validation work. Also, your model's 547 547 :ref:`Validating objects <validating-objects>` for more information on the 548 548 model's ``clean()`` hook. 549 549 550 .. _modelforms-factory: 551 552 ModelForm factory function 553 -------------------------- 554 555 You can create forms from a given model using the standalone function 556 :class:`~django.forms.models.modelform_factory`, instead of using a class 557 definition. This may be more convenient if you do not have many customizations 558 to make:: 559 560 >>> from django.forms.models import modelform_factory 561 >>> BookForm = modelform_factory(Book) 562 563 This can also be used to make simple modifications to existing forms, for 564 example by specifying which fields should be displayed:: 565 566 >>> Form = modelform_factory(Book, form=BookForm, fields=("author",)) 567 568 ... or which fields should be excluded:: 569 570 >>> Form = modelform_factory(Book, form=BookForm, exclude=("title",)) 571 572 You can also specify the widgets to be used for a given field:: 573 574 >>> from django.forms import Textarea 575 >>> Form = modelform_factory(Book, form=BookForm, widgets={"title": Textarea()}) 576 550 577 .. _model-formsets: 551 578 552 579 Model formsets … … with the ``Author`` model. It works just like a regular formset:: 575 602 <tr><th><label for="id_form-0-birth_date">Birth date:</label></th><td><input type="text" name="form-0-birth_date" id="id_form-0-birth_date" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr> 576 603 577 604 .. note:: 578 ``modelformset_factory`` uses ``formset_factory`` to generate formsets. 579 This means that a model formset is just an extension of a basic formset 580 that knows how to interact with a particular model. 605 606 :func:`~django.forms.models.modelformset_factory` uses ``formset_factory`` 607 to generate formsets. This means that a model formset is just an extension 608 of a basic formset that knows how to interact with a particular model. 581 609 582 610 Changing the queryset 583 611 --------------------- … … Providing initial values 631 659 As with regular formsets, it's possible to :ref:`specify initial data 632 660 <formsets-initial-data>` for forms in the formset by specifying an ``initial`` 633 661 parameter when instantiating the model formset class returned by 634 ``modelformset_factory``. However, with model formsets, the initial values only 635 apply to extra forms, those that aren't bound to an existing object instance. 662 :func:`~django.forms.models.modelformset_factory`. However, with model 663 formsets, the initial values only apply to extra forms, those that aren't bound 664 to an existing object instance. 636 665 637 666 .. _saving-objects-in-the-formset: 638 667 … … Limiting the number of editable objects 678 707 --------------------------------------- 679 708 680 709 As with regular formsets, you can use the ``max_num`` and ``extra`` parameters 681 to ``modelformset_factory`` to limit the number of extra forms displayed. 710 to :func:`~django.forms.models.modelformset_factory` to limit the number of 711 extra forms displayed. 682 712 683 713 ``max_num`` does not prevent existing objects from being displayed:: 684 714 … … a particular author, you could do this:: 853 883 >>> formset = BookFormSet(instance=author) 854 884 855 885 .. note:: 856 ``inlineformset_factory`` uses ``modelformset_factory`` and marks 886 ``inlineformset_factory`` uses 887 :func:`~django.forms.models.modelformset_factory` and marks 857 888 ``can_delete=True``. 858 889 859 890 .. seealso::