django wizard, using form and formset in the same step
Asked Answered
R

1

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,

Rodeo answered 1/2, 2013 at 10:33 Comment(1)
Have you looked at django-form-wizard? It supports formsets.Foliaceous
S
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

Spleeny answered 27/3, 2013 at 13:53 Comment(1)
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. The FormContainer is definitely worthy to become a part of Django Framework!Clientele

© 2022 - 2024 — McMap. All rights reserved.