0

我有一个用于创建客户的 CreateView,但我还需要与该客户一起创建一个“识别”模型。我有一个识别模型,该模型具有模型的外键,因为我们需要能够向某些(驾驶执照、护照等)添加任意数量的 ID

无论如何,当前代码(仅创建新客户)如下所示:

class CustomerCreationView(CreateView):
    template_name = "customers/customer_information.html"
    form_class = CustomerInformationForm

    def get_context_data(self, *args, **kwargs):
        context_data = super(CustomerCreationView, self).get_context_data(*args, **kwargs)

        context_data.update({
            'new_customer': True,
        })

        return context_data

CustomerInformationForm 是 ModelForm。我想为标识创建另一个 ModelForm,但我不知道如何将第二个表单添加到 CreateView。我找到了这篇文章,但它已经有 5 年历史了,而不是在谈论 CreateView。

4

2 回答 2

7

您可以使用CreateWithInlinesViewfrom django-extra-views。代码如下所示:

from extra_views import CreateWithInlinesView, InlineFormSet


class IdentificationInline(InlineFormSet):
    model = Identification


class CustomerCreationView(CreateWithInlinesView):
    model = CustomerInformation
    inlines = [IdentificationInline]
于 2014-06-11T22:16:05.103 回答
0
class CustomerCreationView(CreateView):
    template_name = "customers/customer_information.html"
    form_class = CustomerInformationForm
    other_form_class = YourOtherForm

    def get_context_data(self, *args, **kwargs):
        context_data = super(CustomerCreationView, self).get_context_data(*args, **kwargs)

        context_data.update({
            'new_customer': True,
            'other_form': other_form_class,    
        })

        return context_data
于 2013-06-18T21:31:37.827 回答