diff options
| author | Tianyi Wang <wty52133@gmail.com> | 2013-10-15 11:55:43 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-10-15 07:37:51 -0400 |
| commit | 1acd5fc9d293ac07adbb38d11c415dbadd431353 (patch) | |
| tree | 94bf0d0faf718766fec7c35ee533bc5cfac0e74d /docs/topics/forms | |
| parent | 6e7de2edb0565e0ea2fd4ff6701e97ead67138b9 (diff) | |
[1.5.x] Fixed #21006 -- Added example code for overriding methods on an InlineFormSet.
Thanks treysta at gmail.com for the suggestion.
Backport of 9bfe66164e from master
Diffstat (limited to 'docs/topics/forms')
| -rw-r--r-- | docs/topics/forms/modelforms.txt | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 34fb9b957d..7697441a96 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -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 ----------------------- @@ -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 ------------------------------------------- |
