Skip to content

Commit

Permalink
[1.5.x] Fixed #21006 -- Added example code for overriding methods on …
Browse files Browse the repository at this point in the history
…an InlineFormSet.

Thanks treysta at gmail.com for the suggestion.

Backport of 9bfe661 from master
  • Loading branch information
Tianyi Wang authored and timgraham committed Oct 15, 2013
1 parent 6e7de2e commit 1acd5fc
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions docs/topics/forms/modelforms.txt
Expand Up @@ -789,6 +789,7 @@ class's ``clean`` method::
# example custom validation across forms in the formset:
for form in self.forms:
# your custom formset validation
...

Using a custom queryset
-----------------------
Expand Down Expand Up @@ -909,13 +910,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
-------------------------------------------

Expand Down

0 comments on commit 1acd5fc

Please sign in to comment.