Created
May 26, 2010 03:10
-
-
Save ashok-raavi/414009 to your computer and use it in GitHub Desktop.
creating django formset with form that takes parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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