well, to me this is very common - I need to edit __init__ for about 50% of the forms I´ve used so far (and I´ve used quite a lot of forms). I guess that a lot of people will be glad to have "modelforms", but I doubt that they can be used in real-world- applications. and ... it´s ok, if django doesn´t focus on solving complex issues - but then it should be explained like this.
from the django mainpage: "Django was designed to handle two challenges: the intensive deadlines of a newsroom and the stringent requirements of the experienced Web developers who wrote it. It lets you build high-performing, elegant Web applications quickly." I don´t think that´s true (anymore). however, here´s the (quite ugly) working solution: FORM class BaseUploadFormSet(BaseFormSet): def __init__(self, **kwargs): self.path_server = kwargs['path_server'] self.path = kwargs['path'] del kwargs['path_server'] del kwargs['path'] super(BaseUploadFormSet, self).__init__(**kwargs) def _construct_form(self, i, **kwargs): 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, *args, **kwargs): self.path_server = kwargs['path_server'] self.path = kwargs['path'] del kwargs['path_server'] del kwargs['path'] super(UploadForm, self).__init__(*args, **kwargs) VIEW UploadFormSet = formset_factory(UploadForm, formset=BaseUploadFormSet, extra=5) if request.method == 'POST': formset = UploadFormSet(data=request.POST, files=request.FILES, path_server=PATH_SERVER, path=path) ... else: formset = UploadFormSet(path_server=PATH_SERVER, path=path) On Aug 21, 8:45 pm, "Justin Fagnani" <[EMAIL PROTECTED]> wrote: > Looks like you're almost there. You fixed a couple of problems in the > code I posted. > Can you post more of the error so we know which __init__ is thowing > the exception? > > I know it seems complicated, but I'd be surprised if this pattern was > common. I doubt most Form subclasses require extra arguments to > __init__(). A general way to do this might be nice, but could be very > complicated. > > Another way to do this would be to create your formset, and then > iterate over formset.forms and set the attributes you need, and then > do what you need with the formset. > > -Justin > > On Thu, Aug 21, 2008 at 6:42 AM, patrickk <[EMAIL PROTECTED]> wrote: > > > hehe :-) > > > doesn´t work when data is posted: > > > if request.method == 'POST': > > formset = UploadFormSet(path_server=PATH_SERVER, path=path, > > request.POST, request.FILES) > > > error: __init__() got multiple values for keyword argument 'path' > > > On Aug 21, 2:55 pm, patrickk <[EMAIL PROTECTED]> wrote: > >> 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 django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---