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