lundi 30 mars 2015

Django form wizard - variable number of forms

I'm trying to create a form wizard that would contain variable number of questionnaires, depending on how many of them are present in the database and marked as active. For every one of them I am using this form, and the same template. This is my form:



class QuestionnaireForm(forms.Form):

def __init__(self, slug='', *args, **kwargs):
super(QuestionnaireForm, self).__init__(*args, **kwargs)

degrees = Questionnaire.objects.get(slug=slug).degrees

VALUES = ()
for i in range(1,degrees+1):
VALUES += ((i,i),)

items = Item.objects.filter(Q(scales__questionnaire__slug=slug)|Q(scales__slug=slug)).order_by('ord_number')
for item in items:
self.fields[unicode(item.id)] = forms.ChoiceField(
choices=VALUES,
required=True,
widget=RadioSelect,
error_messages={'required': 'Bro, you have to answer that.'},
label = item.name)


Now, since I need to provide a slug to this form in order to get different questionnaire every time, I tried something like this:



class Testing(SessionWizardView):
form_list = [QuestionnaireForm(slug=questionnaire.slug) for questionnaire in Questionnaire.objects.filter(active=True)]
template_name = 'index.html'
success_url = '/'


It gives me the following error: issubclass() arg 1 must be a class. Obviously, I'm not passing slug in the proper place, but I'm not sure where I should be passing it. My guess is that I should be overriding some SessionWizardView method, but I'm not having any luck (skill?) so far.


Aucun commentaire:

Enregistrer un commentaire