Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.6.x] Fixed #21006 -- Added example code for overriding methods on …

…an InlineFormSet.

Thanks treysta at gmail.com for the suggestion.

Backport of 9bfe661 from master
  • Loading branch information...
commit ecfd958c84a2b224ff1b71c42cfe481da98c14e6 1 parent 07ebd23
@tianyi33 tianyi33 authored timgraham committed
Showing with 26 additions and 4 deletions.
  1. +26 −4 docs/topics/forms/modelforms.txt
View
30 docs/topics/forms/modelforms.txt
@@ -919,7 +919,7 @@ class's ``clean`` method::
# example custom validation across forms in the formset:
for form in self.forms:
# your custom formset validation
- pass
+ ...
Using a custom queryset
-----------------------
@@ -1046,13 +1046,35 @@ a particular author, you could do this::
:ref:`Manually rendered can_delete and can_order <manually-rendered-can-delete-and-can-order>`.
-Overriding ``clean()`` on an ``InlineFormSet``
-----------------------------------------------
+Overriding methods on an ``InlineFormSet``
+------------------------------------------
-See :ref:`model-formsets-overriding-clean`, but subclass
+When overriding methods on ``InlineFormSet``, you should subclass
:class:`~models.BaseInlineFormSet` rather than
:class:`~models.BaseModelFormSet`.
+For example, If you want to override ``clean()``::
+
+ from django.forms.models import BaseInlineFormSet
+
+ class MyModelFormSet(BaseInlineFormSet):
+ def clean(self):
+ super(MyModelFormSet, self).clean()
+ # example custom validation across forms in the formset:
+ for form in self.forms:
+ # your custom formset validation
+ ...
+
+See also :ref:`model-formsets-overriding-clean`.
+
+Then when you create your inline formset, pass in the optional argument
+``formset``::
+
+ >>> from django.forms.models import inlineformset_factory
+ >>> BookFormSet = inlineformset_factory(Author, Book, formset=MyModelFormSet)
+ >>> author = Author.objects.get(name=u'Mike Royko')
+ >>> formset = BookFormSet(instance=author)
+
More than one foreign key to the same model
-------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.