django wizard, using form and formset in the same step

﹥>﹥吖頭↗ 提交于 2019-11-30 04:06:27

问题


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



来源:https://stackoverflow.com/questions/14644357/django-wizard-using-form-and-formset-in-the-same-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!