django wizard, using form and formset in the same

2020-03-02 04:47发布

问题:

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,

回答1:

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