通过向导步骤将Django表单和表单集结合在一起

时间:2019-05-02 12:47:44

标签: python django formset django-formwizard

我在Django中有一个表单向导。在第一步中,我需要插入有关传输请求的数据和一条消息,该消息在另一个模型中。在第二步中,我使用表单集添加乘客,在最后一个表单中,地址也添加在表单集中。

我不知道如何在第一步中插入消息字段。我试图在上下文中添加一个表单集,该上下文覆盖了SessionWizardView的get_context_data方法(下面的代码)。它显示了该字段,但是我无法弄清楚如何保存它。

我看到了这篇文章Django form with fields from two different models,但是SessionWizardView不允许我一步一步地添加带有两个表单的视图,我只能添加表单。

我也读过这个django wizard, using form and formset in the same step,但是它不起作用,也许是因为它是6年前的。

我最后一次尝试是在传输模型窗体中添加一个自定义字段(不在下面的代码中),但是我认为这不是正确的方法(我仍然不知道如何保存该消息)方式)。

代码如下:

models.py

class Transporte(models.Model):
    #fields about transport here

class Mensagem(models.Model):
    #this is the message field
    mensagem = models.TextField("Mensagem", blank=True)
    transporte = models.ForeignKey(Transporte, on_delete=models.CASCADE)

...

forms.py

class FormTransporte(forms.ModelForm):
    class Meta:
        model = Transporte

class FormMensagem(forms.ModelForm):
    class Meta:
        model = Mensagem
        exclude = ()

FormsetMensagem = inlineformset_factory(Transporte, Mensagem, form=FormMensagem,extra=1)
...

views.py

FORMS = [("base", FormTransporte),
         ("passageiros",FormsetPassageiros),
         ("enderecos",FormsetEnderecos)
         ]

TEMPLATES = {"base":'transporte/solicitar.html',
             "passageiros":'transporte/passageiros.html',
             "enderecos":'transporte/enderecos.html'}

class WizardView(SessionWizardView):
    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def get_context_data(self,form, **kwargs):
    #Here I try to append the formset in the first step. It shows the
    #field, but no success on saving it
        data = super(WizardView,self).get_context_data(form,**kwargs)
        if self.steps.current == "base":
            if self.request.POST:
                data['mensagem'] = FormsetMensagem(self.request.POST)
            else:
                data['mensagem'] = FormsetMensagem()
        return data;

    def done(self,form_list,form_dict,**kwargs):
        form_transporte = form_dict['base']
    #the line below doesn't works
        form_mensagem = form_dict['base']['mensagem']
        instance_transporte = form_transporte.save()

        if 'enderecos' in form_dict:
            formset_endereco = form_dict['enderecos']
            for form_endereco in formset_endereco:
                endereco = form_endereco.save(commit=False)
                endereco.transporte = instance_transporte
                endereco.save()

        if 'passageiros' in form_dict:
            formset_passageiros = form_dict['passageiros']
            for form_passageiros in formset_passageiros:
                passageiros = form_passageiros.save(commit=False)
                passageiros.transporte = instance_transporte
                passageiros.save()

        return render(self.request,'transporte/done.html',{
            'form_data': [form.cleaned_data for form in form_list],
        })

...

urls.py

app_name = 'transporte'

urlpatterns = [
    ...
    path(r'solicitar/',login_required(views.WizardView.as_view(views.FORMS, condition_dict={'passageiros': views.tem_passageiros})), name='solicitar'),
    ...
]

有什么想法吗?

0 个答案:

没有答案