7

I have a scenario which I'm trying to plan to start coding and I'm thinking to use django wizard.

My plan is to build a django wizard with two steps, the first simple but the second a bit more complicated. The second step will contain a form that will reshape based on value selected from the first step, which I can see myself doing. I already explored all the existing functionality and I think it can be done easily.

The challenge I'm facing though, is that in the second step itself. I have a form and a formset, the formset is one to many to form (Article -> Images) so when reaching the second step the user will be able to upload one or more images to the same article.

I tried to search everywhere on google mailing lists and stackoverflow for passing a formset to the django wizard class but it seems like you can not pass two forms in the same step.

NewItemWizard.as_view([
    ('category',    CategorySelectionForm),
    ('article',        ArticleForm)
])

as seen, in the example code above, I would like to be able to pass both ArticleForm and ImageFormset to the second step. Is there a way to do this out of the box?

Based on what I'm reading, I believe using a function like get_context_data could help, but it will be very hacky.

def get_context_data(self, form, **kwargs):
    context = super(NewItemWizard, self).get_context_data(form=form, **kwargs)
    if self.steps.current == 'article':
        context.update({
            'image_formset': ImageFormset()
        })
    return context

Anyone can advise for a better approach?

Cheers,

Vladir Parrado Cruz
  • 2,301
  • 21
  • 27
Mo J. Mughrabi
  • 6,747
  • 16
  • 85
  • 143
  • Have you looked at [django-form-wizard](https://github.com/stephrdev/django-formwizard)? It supports formsets. – Nick Feb 01 '13 at 17:32

1 Answers1

7

There is no straight API to do it yet, but there is a clean solution like the one mentioned here:

https://code.djangoproject.com/ticket/18830

mariodev
  • 13,928
  • 3
  • 49
  • 61
  • **The nice part of this approach is that you can use it not only for wizards, but for `generic views` such as `FormView` too. Everyone should probably checkout this [code example](https://code.djangoproject.com/attachment/ticket/18830/form_container.py).** The `FormContainer` is definitely worthy to become a part of Django Framework! – Ivan Kharlamov Apr 24 '13 at 18:39