got it - passing parameters to formsets. this is just my personal
opinion, but in my view it´s overly complicated for such a common
thing ... I thought newforms will make things easier (but that was
about 4 weeks ago).
class BaseUploadFormSet(BaseFormSet):
def __init__(self, path_server=None, path=None, **kwargs):
self.path_server = path_server
self.path = path
super(BaseUploadFormSet, self).__init__(**kwargs)
def _construct_form(self, i, **kwargs):
# this works because BaseFormSet._construct_form() passes
**kwargs
# to the form's __init__()
kwargs["path_server"] = self.path_server
kwargs["path"] = self.path
return super(BaseUploadFormSet, self)._construct_form(i,
**kwargs)
class UploadForm(forms.Form):
def __init__(self, path_server=None, path=None, *args, **kwargs):
self.path_server = path_server
self.path = path
print self.path_server
print self.path
super(UploadForm, self).__init__(*args, **kwargs)
...
define fields here
...
On Aug 21, 2:30 pm, patrickk <[EMAIL PROTECTED]> wrote:
> hmm, this is getting funny ...
>
> def _construct_forms(self):
> # instantiate all the forms and put them in self.forms
> self.forms = []
> for i in xrange(self._total_form_count):
> self.forms.append(self._construct_form(i))
> print self.forms
>
> def _construct_form(self, i, **kwargs):
> """
> Instantiates and returns the i-th form instance in a formset.
> """
> defaults = {'auto_id': self.auto_id, 'prefix':
> self.add_prefix(i)}
> if self.data or self.files:
> defaults['data'] = self.data
> defaults['files'] = self.files
> if self.initial:
> try:
> defaults['initial'] = self.initial[i]
> except IndexError:
> pass
> # Allow extra forms to be empty.
> if i >= self._initial_form_count:
> defaults['empty_permitted'] = True
> defaults.update(kwargs)
> form = self.form(**defaults)
> self.add_fields(form, i)
> print form
> return form
>
> as you can see, I´ve inserted print in _construct_form and
> _construct_forms. the print-statement at the end of _construct_form
> displays the form, but the print-statement in _construct_forms
> displays: [None, None, None, None, None].
>
> On Aug 21, 1:36 pm, patrickk <[EMAIL PROTECTED]> wrote:
>
> > to be more precise:
> > when I do "print form" in formsets.py in line 96, at the end of
> > _construct_form, the form is there.
> > but when trying to display the form(s) in the template, the formset is
> > empty ...
>
> > On Aug 21, 12:19 pm, patrickk <[EMAIL PROTECTED]> wrote:
>
> > > thanks justin.
>
> > > I´m able to pass the paramters to the form now. unfortunately, the
> > > form (in the template) is empty now ... no fields at all, so I guess
> > > there´s something missing here (but I don´t know what).
>
> > > On Aug 21, 11:34 am, "Justin Fagnani" <[EMAIL PROTECTED]>
> > > wrote:
>
> > > > On Thu, Aug 21, 2008 at 12:30 AM, patrickk <[EMAIL PROTECTED]> wrote:
>
> > > > > I´m not sure (anymore) we´re all talking about the same issue.
>
> > > > I think we are. I'll see if I can clarify... The broad idea is that
> > > > you pass the parameters to the formset in your view via an overridden
> > > > __init__(), then you pass them to the form via an overridden
> > > > _construct_form().
>
> > > > Something like this:
>
> > > > class MyBaseFormSet(BaseFormSet):
> > > > def __init__(self, foo=None, **kwargs):
> > > > self.foo = foo
> > > > super(BaseFormSet, self).__init__(**kwargs)
>
> > > > def _construct_form(self, i, **kwargs):
> > > > # this works because BaseFormSet._construct_form() passes **kwargs
> > > > # to the form's __init__()
> > > > super(BaseFormSet, self)._construct_form(i, **{'foo': self.foo})
>
> > > > class MyForm(Form):
> > > > def __init__(self, foo=None, *args, **kwargs):
> > > > self.foo = foo
> > > > super(BaseForm, self).__init__(*args, **kwargs)
>
> > > > MyFormSet = formset_factory(MyForm, formset=MyBaseFormSet)
>
> > > > def view_func(request):
> > > > formset = MyFormSet(foo='bar')
> > > > ...
>
> > > > hope that helps,
> > > > Justin
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---