我有一个CreateView用于创建客户,但我还需要与该客户一起创建“识别”模型。我有一个识别模型,它具有模型的外键,因为我们需要能够为某些ID添加任何数量的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。我找到了this article,但它已经有5年了,并没有谈到CreateView。
答案 0 :(得分:6)
您可以使用django-extra-views中的CreateWithInlinesView
。代码如下所示:
from extra_views import CreateWithInlinesView, InlineFormSet
class IdentificationInline(InlineFormSet):
model = Identification
class CustomerCreationView(CreateWithInlinesView):
model = CustomerInformation
inlines = [IdentificationInline]
答案 1 :(得分:-1)
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
我认为这应该有用..我正在工作,我无法测试..