Hi All,

(Sorry for the cross post to Stackoverflow as well - I'm really
pulling my hair out on this one!)
Posted on 
http://stackoverflow.com/questions/8172398/django-formwizard-how-do-you-dynamically-create-a-formset-based-on-previous-st

I've seen [this][1] post and it's not working (in part because it's
dated).  I've also studied the source tree to no avail (the tests
helped) but I can't find my answer. What I'm looking to do is get a
seed set of data in form0 ('start') which will dynamically build a
formset for step2.  Step 2 is simply a verification step.

  1. 'start' - User enters subdivision (subA), zipcode (12345) and a
csv of lots (51,52,53)
  2. 'step2' - A dynamic form (modelformset) is created with 3 forms
representing 51,52,53
  3. User hits go and the models are built

i.e.

    data = [ { 'subdivision': <subA>, 'zipcode': '12345',
'lot_number': '51'}
             { 'subdivision': <subA>, 'zipcode': '12345',
'lot_number': '52'}
             { 'subdivision': <subA>, 'zipcode': '12345',
'lot_number': '53'} ]

__What I've tried__

When implementing the solution [here][1] I only get `data=None`.  This
is dated and looking through the source I thought the "right" way to
do this was to simply override the `get_form_instance` method and feed
it`get_cleaned_data_for_step`,  but that appears to revalidate and do
_a lot_ more stuff than what I think it needs to (and it didn't work).

So.. What I'm looking for is two things.

 1. What is the right way to get the previous forms data.
 2. How do I take that data and use it to create a n-number of
formsets.

FWIW I am using Django 1.4-alpha formset wizard.

Here is what I have.

    # urls.py
        url(r'homes/bulk/$', TestWizard.as_view([('start',
BulkHomeForm0),
                                                 ('step2',
HomeFormSet)])),

    # Models.py
    class Subdivision(models.Model):
        name = models.CharField(max_length=64)

    class Home(models.Model):
        lot_number = models.CharField(max_length=16)
        subdivision = models.ForeignKey(Subdivision)
        zipcode = models.IntegerField(validators=[validate_zipcode],
null=True)

    # Forms
    class BulkHomeForm0(forms.Form):
        subdivision =
forms.ModelChoiceField(queryset=Subdivision.objects.all(),
required=True)
        zipcode = USZipCodeField(required=True)
        lots = forms.CharField(max_length=5000,
widget=forms.Textarea()

        def clean(self):
            subdivision = self.cleaned_data.get('subdivision', False)
            zipcode = self.cleaned_data.get('zipcode', False)
            final_data = []
            for item in self.cleaned_data.get('lots', "").split(",")
                final_data.append({'subdivision':subdivision,
                                   'zipcode':zipcode,
                                   'lot_number':item})
            self.cleaned_data['homes'] = final_data

    class BulkHomeForm1(forms.ModelForm):
        class Meta:
            model = Home

    HomeFormSet = modelformset_factory(Home, form=BulkHomeForm1,
extra=2)

    # Views.py
    class TestWizard(WizardView):
        storage_name =
'django.contrib.formtools.wizard.storage.session.SessionStorage'

        def get_form(self, step=None, data=None, files=None):

            if step is None:
                step = self.steps.current
            if step == 'step2':
                print "Entering Step 2 Form"
            form = super(TestWizard, self).get_form(step=step,
data=data, files=files)
            if self.steps.current == 'step2':
                print "Form is now contructed"

            return form

        def get_context_data(self, form, **kwargs):
            context = super(TestWizard, self).get_context_data(form,
**kwargs)
            if self.steps.current == 'start':
                print "Gathering context data for start"
                context.update({'another_var': True})
            if self.steps.current == 'step2':
                print "Gathering context data for step 2"
                context.update({'another_var2': True})
            return context

        def get_form_instance(self, step):
            if step == 'step2':
                print "Getting instance data for step2"
                # return self.get_cleaned_data_for_step('start')
['homes']
            return self.instance_dict.get(step, None)

        def get_form_initial(self, step):
            if step == 'step2':
                print "Getting initial data for step2"
                # return self.get_cleaned_data_for_step('start')
['homes']
            else: self.initial_dict.get(step, {})


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



  [1]: 
http://stackoverflow.com/questions/3940138/how-to-pass-previous-form-data-to-the-constructor-of-a-dynamicform-in-formwizard

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to