FormSet in Wizard

147 views
Skip to first unread message

Austin Gabel

unread,
Feb 4, 2009, 10:18:00 AM2/4/09
to django...@googlegroups.com
Hello,

I am trying to set up a Django FormWizard to add users to my site.  On one of the steps I need to be able to enter more than one address.  I was hoping to use a FormSet on this step but it does not seem to work as I had expected.  The FormSet displays correctly initially, but when I submit the form I get an error saying that the FormSet is not iterable.  Has anyone made this work before?

Yuka

unread,
Apr 2, 2009, 10:11:50 AM4/2/09
to Django users
Hi Austin,

I dont know if you still need a solution, but I did, and someone else
might -- also for the sake of completion, here is what i cooked up:
Just inherit from below class, instead of FormWizard, and start
feeding it FormSet classes as one sees fit. Also, there might be
better solutions, e.g. one could probably get away with only
overriding the get_form method, but i figured that would be a little
bit trickier.
This suited my needs just fine. Alas, enjoy.

--
from django.contrib.formtools.utils import security_hash

try:
import cPickle as pickle
except ImportError:
import pickle

from django.conf import settings
from django.utils.hashcompat import md5_constructor
from django.forms import BooleanField

class FormSetWizard(FormWizard):
def render(self, form, request, step, context=None):
old_data = request.POST
prev_fields = []
if old_data:
hidden = forms.HiddenInput()
for i in range(step):
old_form = self.get_form(i, old_data)
hash_name = 'hash_%s' % i
if isinstance(old_form, BaseFormSet):
for f in old_form.forms +
[old_form.management_form]:
prev_fields.extend([bf.as_hidden() for bf in
f])
else:
prev_fields.extend([bf.as_hidden() for bf in
old_form])
prev_fields.append(hidden.render(hash_name,
old_data.get(hash_name, self.security_hash(request, old_form))))
return self.render_template(request, form, ''.join
(prev_fields), step, context)

def security_hash(self, request, form):
def security_func(request, form, *args):
data = []
for f in form.forms + [form.management_form]:
data.extend([(bf.name, bf.field.clean(bf.data) or '')
for bf in f])
data.extend(args)
data.append(settings.SECRET_KEY)
pickled = pickle.dumps(data, pickle.HIGHEST_PROTOCOL)
return md5_constructor(pickled).hexdigest()
if isinstance(form, BaseFormSet):
return security_func(request, form)
return security_hash(request, form)
--
Best Regards, Yuka Poppe.
Reply all
Reply to author
Forward
0 new messages