django 向导,在同一步骤中使用 form 和 formset [英] django wizard, using form and formset in the same step

查看:20
本文介绍了django 向导,在同一步骤中使用 form 和 formset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,我正在尝试计划开始编码,我正在考虑使用 django 向导.

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

我的计划是用两个步骤构建一个 django 向导,第一个简单但第二个稍微复杂一些.第二步将包含一个表单,该表单将根据从第一步中选择的值进行重塑,我可以看到自己正在这样做.我已经探索了所有现有的功能,我认为它可以轻松完成.

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.

我试图在 google 邮件列表和 stackoverflow 上到处搜索以将表单集传递给 django 向导类,但似乎您无法在同一步骤中传递两个表单.

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)
])

如上所示,在上面的示例代码中,我希望能够将 ArticleForm 和 ImageFormset 都传递到第二步.有没有办法做到这一点?

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?

根据我正在阅读的内容,我相信使用像 get_context_data 这样的函数会有所帮助,但它会非常笨拙.

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?

干杯,

推荐答案

目前还没有直接的 API 来做这件事,但是有一个像这里提到的那样的干净的解决方案:

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

这篇关于django 向导,在同一步骤中使用 form 和 formset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆