Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21539 -- Added example of modelformset_factory's form argument

  • Loading branch information...
commit 1fa681ee11ac234cdc515338397625f514e85b73 1 parent 164a3a3
Alasdair Nicol alasdairnicol authored timgraham committed
Showing with 24 additions and 0 deletions.
  1. +24 −0 docs/topics/forms/modelforms.txt
24 docs/topics/forms/modelforms.txt
View
@@ -760,6 +760,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-fields-with-fields-and-exclude:
Please sign in to comment.
Something went wrong with that request. Please try again.