Question

I want to use a formset within a formwizard.

class Model1(models.Model):
    customerid = models.CharField(max_length=20)
    Name = models.CharField(max_length=40)

class Model1Form(ModelForm):
    class Meta:
            model = Model1

class Model2(models.Model):
    product = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=5, decimal_places=2)

class Model2Form(forms.Form):
    product = forms.ModelChoiceField(queryset=Model2.objects.all())
    amount = forms.IntegerField(required=False)

Model2Formset = formsets.formset_factory(Model2Form)

In my urls.py:

    (r'^testwizard/$', TestWizard.as_view([Model1Form, Model2Formset])),

I use a basic view to see the results of posting the form:

class TestWizard(SessionWizardView):
def done(self, form_list, **kwargs):
    return render_to_response('template', {
        'form_data': [form.cleaned_data for form in form_list],
    })

When the formset has multiple entries I can only see one entry:

 {'customerid': u'7676', 'Name': u'7', 'klantnummer': u'7'} [{'product': <Model2: Bike>, 'amount': 7}]

I expected:

{'customerid': u'7676', 'Name': u'7', 'klantnummer': u'7'} [{'product': <Model2: Bike>, 'amount': 7},{'product': <Model2: Plane>, 'amount': 5}]

In the documentation is found this:

WizardView supports ModelForms and ModelFormSets. Additionally to initial_dict, the >as_view() method takes an instance_dict argument that should contain instances of >ModelForm and ModelFormSet. Similarly to initial_dict, these dictionary key values should >be equal to the step number in the form list.

Unfortunately I am not sure what is meant here.

Was it helpful?

Solution

This didnt work, because I needed an inline formset documented here: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top