开发者

Formwizard with one model and inlineformset

开发者 https://www.devze.com 2023-03-09 09:11 出处:网络
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 te

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开发者_Go百科 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号