1

您将如何处理有两个单独的表单和两个模型但共享相同的 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?
4

0 回答 0