Skip to content

Instantly share code, notes, and snippets.

@ashok-raavi
Created May 26, 2010 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashok-raavi/414009 to your computer and use it in GitHub Desktop.
Save ashok-raavi/414009 to your computer and use it in GitHub Desktop.
creating django formset with form that takes parameters
from django import forms
class FooForm(forms.Form):
def __init__(self, arg1, *args, **kwargs):
self.arg1 = arg1
super(FooForm, self).__init__(*args, **kwargs)
# form fields here
# define the custom formset here
from django.forms.formsets import BaseFormSet
class FooBaseFormSet(BaseFormSet):
def __init__(self, arg1, *args, **kwargs):
self.arg1 = arg1
super(FooBaseFormSet, self).__super__(*args, **kwargs)
def _construct_forms(self):
self.forms = []
for i in xrange(self.total_form_count()):
self.forms.append(self._construct_form(i, arg1=self.arg1))
# creating the formset for FooForm
from django.forms.formsets import formset_factory
foo_formset = formset_factory(FooForm, formset=FooBaseFormSet, extra=2)
foo_forms = foo_formset(arg1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment