diff options
| author | Alasdair Nicol <alasdair@thenicols.net> | 2013-12-01 02:05:15 +0000 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-12-14 16:36:27 -0500 |
| commit | e4174da34ab239f04d9c79fec9822b01d63e453b (patch) | |
| tree | becc7aea9e3ab342afc845b76b4e976e8208c4bc /docs | |
| parent | a53820b1b1747d6f9b5ee5723aa7526ededdaba7 (diff) | |
[1.5.x] Fixed #21539 -- Added example of modelformset_factory's form argument
Backport of 1fa681ee11 from master
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/forms/modelforms.txt | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 7f9d12d5d7..936a6cd68c 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -646,6 +646,30 @@ instances of the model, you can specify an empty QuerySet:: >>> AuthorFormSet(queryset=Author.objects.none()) +Changing the ``form`` +--------------------- + +By default, when you use ``modelformset_factory``, a model form will +be created using :func:`~django.forms.models.modelform_factory`. +Often, it can be useful to specify a custom model form. For example, +you can create a custom model form that has custom validation:: + + class AuthorForm(forms.ModelForm): + class Meta: + model = Author + fields = ('name', 'title') + + def clean_name(self): + # custom validation for the name field + ... + +Then, pass your model form to the factory function:: + + AuthorFormSet = modelformset_factory(Author, form=AuthorForm) + +It is not always necessary to define a custom model form. The +``modelformset_factory`` function has several arguments which are +passed through to ``modelform_factory``, which are described below. Controlling which fields are used with ``fields`` and ``exclude`` ----------------------------------------------------------------- |
