0

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.

El Gato Loco
  • 33
  • 1
  • 9

1 Answers1

0

After doing some more research, I finally managed to do what I wanted. Solution proposed here did the trick, even though I'm not sure why is this working.

So, here's my modified code. Class generator from the cited answer:

def class_generator(cls, **additionalkwargs):
    class ClassWithKwargs(cls):
        def __init__(self, *args, **kwargs):
            kwargs.update(additionalkwargs)
            super(ClassWithKwargs, self).__init__(*args, **kwargs)
    return ClassWithKwargs

Which I then applied like this:

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

Since I'm not a pro (more of like an advancing noob), if there's someone who could clarify what's happening here, that would be nice for the sake of future readers.

Community
  • 1
  • 1
El Gato Loco
  • 33
  • 1
  • 9