具有一个模型和内联表单集的表单向导
我正在使用 Formwizard 开发一个与一个模型匹配的向导。 另外,由于我的模型和其他模型之间的关系,我使用 inlineformset_factory 将字段显示在模板中。
我已经为模型属性创建了两个具有相同模型的表单。我刚刚更改了 fields 属性以区分我在向导中使用的两种表单。
目前,为了在我的向导中保存表单信息,我正在这样做:
def done(self, request, form_list):
instance = Sale()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.save()
这效果很好,但不会保存我的内联表单集
所以我在完成方法中添加了这个:
picture_formset = ProductPictureFormset(request.POST, instance=instance)
if picture_formset.is_valid():
picture_formset.save()
但是当我这样做时,我在 Django 中遇到了这个错误:
Exception Type: ValidationError
我有注意到我的内联表单集中包含的数据没有在步骤之间传递。 这就是我现在在向导中添加内联表单集的方式:
def parse_params(self, request, *args, **kwargs):
if self.step == 0:
self.extra_context.update({
'picture_formset': ProductPictureFormset(),
'brand_attribute_formset': BrandAttributeFormset()
})
但似乎我必须找到一种方法将这些表单中检索到的数据传递到第二步。
知道怎么做吗?
谢谢你!
I am developing a wizard using Formwizard that matches one model.
Also due to the relationships between my model and other models, I am using inlineformset_factory to have the fields present in the template.
I have create 2 forms with the same model for the model attribute. I have just changed the fields attribute to differentiate the 2 forms that I using in my wizard.
For the moment to save the forms informations in my wizard I am doing this:
def done(self, request, form_list):
instance = Sale()
for form in form_list:
for field, value in form.cleaned_data.iteritems():
setattr(instance, field, value)
instance.save()
This works well but doesn't save my inlineformsets
So I have added this in the done method:
picture_formset = ProductPictureFormset(request.POST, instance=instance)
if picture_formset.is_valid():
picture_formset.save()
But when I do that I have this error in Django:
Exception Type: ValidationError
I have noticed that my data contained in my inline formsets are not passed between steps.
This is how I am adding the inline formset in my wizard right now:
def parse_params(self, request, *args, **kwargs):
if self.step == 0:
self.extra_context.update({
'picture_formset': ProductPictureFormset(),
'brand_attribute_formset': BrandAttributeFormset()
})
But it seems I have to find a way to pass the data retrieved in these forms to the second step.
Any idead how to do that?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对我的模型进行了一些重构,以便能够在 process_step 的每个步骤中保存每个模型。我还在会话中存储信息。
I have refactored a bit my models to be able to save each models at every step in process_step. Also I am storing infos in the session.