Ticket #10239: ticket10239.patch

File ticket10239.patch, 2.9 KB (added by Will Hardy, 13 years ago)

Added documentation for modelform_factory

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index b34f4d0..3c2530a 100644
    a b def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c  
    139139    ``exclude`` is an optional list of field names. If provided, the named
    140140    fields will be excluded from the returned fields, even if they are listed
    141141    in the ``fields`` argument.
     142
     143    ``widgets`` is a dictionary of model field names mapped to a widget
     144
     145    ``formfield_callback`` is a callable that takes a model field and returns
     146    a form field.
    142147    """
    143148    field_list = []
    144149    ignored = []
    class ModelForm(BaseModelForm):  
    369374
    370375def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
    371376                       formfield_callback=None):
     377    """
     378    Returns a ``SortedDict`` containing form fields for the given model.
     379
     380    ``fields`` is an optional list of field names. If provided, only the named
     381    fields will be included in the returned fields.
     382
     383    ``exclude`` is an optional list of field names. If provided, the named
     384    fields will be excluded from the returned fields, even if they are listed
     385    in the ``fields`` argument.
     386
     387    ``widgets`` is a dictionary of model field names mapped to a widget
     388
     389    ``formfield_callback`` is a callable that takes a model field and returns
     390    a form field.
     391    """
    372392    # Create the inner Meta class. FIXME: ideally, we should be able to
    373393    # construct a ModelForm without creating and passing in a temporary
    374394    # inner class.
  • docs/topics/forms/modelforms.txt

    diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt
    index 6d418a2..be335b6 100644
    a b for more on how field cleaning and validation work. Also, your model's  
    533533:ref:`Validating objects <validating-objects>` for more information on the
    534534model's ``clean()`` hook.
    535535
     536ModelForm factory function
     537--------------------------
     538
     539You can create forms from a given model using the standalone function
     540``modelformset_factory``, instead of using a class definition. This
     541may be more convenient if you do not have many customisations to make::
     542
     543    >>> from django.forms.models import modelform_factory
     544    >>> BookForm = modelform_factory(Book)
     545
     546This can also be used to make simple modifications to existing forms, for
     547example by specifying which fields should be displayed::
     548
     549    >>> Form = modelform_factory(Book, form=BookForm, fields=("author",))
     550
     551... or which fields should be excluded::
     552
     553    >>> Form = modelform_factory(Book, form=BookForm, exclude=("title",))
     554
     555You can also specify the widgets to be used for a given field::
     556
     557    >>> from django.forms import Textarea
     558    >>> Form = modelform_factory(Book, form=BookForm, widgets={"title": Textarea()})
     559
    536560.. _model-formsets:
    537561
    538562Model formsets
Back to Top