Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.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 1acd5fc9d293ac07adbb38d11c415dbadd431353 1 parent 6e7de2e
Tianyi Wang authored timgraham committed

Showing 1 changed file with 26 additions and 3 deletions. Show diff stats Hide diff stats

  1. 29  docs/topics/forms/modelforms.txt
29  docs/topics/forms/modelforms.txt
@@ -789,6 +789,7 @@ class's ``clean`` method::
789 789
             # example custom validation across forms in the formset:
790 790
             for form in self.forms:
791 791
                 # your custom formset validation
  792
+                ...
792 793
 
793 794
 Using a custom queryset
794 795
 -----------------------
@@ -909,13 +910,35 @@ a particular author, you could do this::
909 910
 
910 911
     :ref:`Manually rendered can_delete and can_order <manually-rendered-can-delete-and-can-order>`.
911 912
 
912  
-Overriding ``clean()`` on an ``InlineFormSet``
913  
-----------------------------------------------
  913
+Overriding methods on an ``InlineFormSet``
  914
+------------------------------------------
914 915
 
915  
-See :ref:`model-formsets-overriding-clean`, but subclass
  916
+When overriding methods on ``InlineFormSet``, you should subclass
916 917
 :class:`~models.BaseInlineFormSet` rather than
917 918
 :class:`~models.BaseModelFormSet`.
918 919
 
  920
+For example, If you want to override ``clean()``::
  921
+
  922
+    from django.forms.models import BaseInlineFormSet
  923
+
  924
+    class MyModelFormSet(BaseInlineFormSet):
  925
+        def clean(self):
  926
+            super(MyModelFormSet, self).clean()
  927
+            # example custom validation across forms in the formset:
  928
+            for form in self.forms:
  929
+                # your custom formset validation
  930
+                ...
  931
+
  932
+See also :ref:`model-formsets-overriding-clean`.
  933
+
  934
+Then when you create your inline formset, pass in the optional argument
  935
+``formset``::
  936
+
  937
+    >>> from django.forms.models import inlineformset_factory
  938
+    >>> BookFormSet = inlineformset_factory(Author, Book, formset=MyModelFormSet)
  939
+    >>> author = Author.objects.get(name=u'Mike Royko')
  940
+    >>> formset = BookFormSet(instance=author)
  941
+
919 942
 More than one foreign key to the same model
920 943
 -------------------------------------------
921 944
 

0 notes on commit 1acd5fc

Please sign in to comment.
Something went wrong with that request. Please try again.