Django在CreateView中有多个单独的表单

时间:2016-11-08 14:44:50

标签: django django-models django-forms django-class-based-views django-1.10

您如何处理具有两个单独的表单和两个模型但共享相同的CreateView的情况?

例如,如果您有两个继承一个抽象模型的模型,并且在CreateView中我们在context中包含两个表单。无论用户提交哪种形式,都应该创建其关联模型的实例。

class EventCreateView(generic_view.CreateView):
    template_name = 'event/create.html'
    form_class = OneToOneEventForm
    second_form_class = GroupEventForm

    def get_context_data(self, **kwargs):
        context = super(EventCreateView, self).get_context_data(**kwargs)
        if 'form' not in context:
            context['form'] = self.form_class()
        if 'form2' not in context:
            context['form2'] = self.second_form_class()
        return context

     ## how would we deal with post() and form_valid() and object creation?

0 个答案:

没有答案