From 15ab4936d7d7c351ab8eea5f8c2765632429c9ed Mon Sep 17 00:00:00 2001 From: Alasdair Nicol Date: Sun, 1 Dec 2013 02:05:15 +0000 Subject: [PATCH] [1.6.x] Fixed #21539 -- Added example of modelformset_factory's form argument Backport of 1fa681ee11 from master --- docs/topics/forms/modelforms.txt | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 38784f9566..f513c44854 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -747,6 +747,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`` -----------------------------------------------------------------