Ticket #10239: 10239.diff

File 10239.diff, 7.3 KB (added by timo, 3 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 
    141141    ``exclude`` is an optional list of field names. If provided, the named
    142142    fields will be excluded from the returned fields, even if they are listed
    143143    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.
    144149    """
    145150    field_list = []
    146151    ignored = []
    class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)): 
    371376
    372377def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
    373378                      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    """
    374394    # Create the inner Meta class. FIXME: ideally, we should be able to
    375395    # construct a ModelForm without creating and passing in a temporary
    376396    # 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/ 
    99
    1010   api
    1111   fields
     12   models
    1213   widgets
    1314   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====================
     2Model 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 
    547547:ref:`Validating objects <validating-objects>` for more information on the
    548548model's ``clean()`` hook.
    549549
     550.. _modelforms-factory:
     551
     552ModelForm factory function
     553--------------------------
     554
     555You can create forms from a given model using the standalone function
     556:class:`~django.forms.models.modelform_factory`, instead of using a class
     557definition. This  may be more convenient if you do not have many customizations
     558to make::
     559
     560    >>> from django.forms.models import modelform_factory
     561    >>> BookForm = modelform_factory(Book)
     562
     563This can also be used to make simple modifications to existing forms, for
     564example 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
     572You 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
    550577.. _model-formsets:
    551578
    552579Model formsets
    with the ``Author`` model. It works just like a regular formset:: 
    575602    <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>
    576603
    577604.. 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.
    581609
    582610Changing the queryset
    583611---------------------
    Providing initial values 
    631659As with regular formsets, it's possible to :ref:`specify initial data
    632660<formsets-initial-data>` for forms in the formset by specifying an ``initial``
    633661parameter 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
     663formsets, the initial values only apply to extra forms, those that aren't bound
     664to an existing object instance.
    636665
    637666.. _saving-objects-in-the-formset:
    638667
    Limiting the number of editable objects 
    678707---------------------------------------
    679708
    680709As with regular formsets, you can use the ``max_num`` and ``extra`` parameters
    681 to ``modelformset_factory`` to limit the number of extra forms displayed.
     710to :func:`~django.forms.models.modelformset_factory` to limit the number of
     711extra forms displayed.
    682712
    683713``max_num`` does not prevent existing objects from being displayed::
    684714
    a particular author, you could do this:: 
    853883    >>> formset = BookFormSet(instance=author)
    854884
    855885.. note::
    856     ``inlineformset_factory`` uses ``modelformset_factory`` and marks
     886    ``inlineformset_factory`` uses
     887    :func:`~django.forms.models.modelformset_factory` and marks
    857888    ``can_delete=True``.
    858889
    859890.. seealso::
Back to Top