Using Multiple ModelForms with Class-Based Views

对着背影说爱祢 提交于 2019-12-23 00:48:34

问题


I've got a situation where I'd like to add an additional modelform to my CreateView. We have an entry order system that allows someone to add an order and then add items to that order. Typically, when someone adds an order for the first time they'd like to also add an item to that order, so I want to combine those models into a single form and process them on initial order entry. I'm running into a problem when the forms don't validate.

I've overridden get_context_data to add the item form to the template and I've overridden post to process the extra form. But when the forms are invalid I need to re-render the original form passing in the POST data. What's the preferred way to override get_context_data in order to render the forms with/without the POST data? Should I do something like this?

def get_context_data(self, **kwargs):
    context = super(OrderAdd, self).get_context_data(**kwargs)
    if self.request.method == 'POST':
        item_form = ItemForm(self.request.POST, prefix='item')
    else:
        item_form = ItemForm(prefix='item')
    context['item_form'] = item_form
    return context

Here's my CreateView in it's current form, where I'm currently stuck.

class OrderAdd(CreateView):
    model = Order
    form_class = OrderForm
    context_object_name = 'object'
    template_name = 'form.html'

    def get_context_data(self, **kwargs):
        context = super(OrderAdd, self).get_context_data(**kwargs)
        item_form = ItemForm(prefix='item')
        context['item_form'] = item_form
        return context

    def post(self, request, *args, **kwargs):
        order_form = OrderForm(request.POST)
        item_form = ItemForm(request.POST, prefix='item')

        if order_form.is_valid() and item_form.is_valid():
            return self.form_valid(order_form)
        else:
            context = self.get_context_data()
            return render(self.request, 'form.html', context)

回答1:


One slightly inelegant approach:

def get_context_data(self, **kwargs):
    context = super(OrderAdd, self).get_context_data(**kwargs)
    if self.request.method == 'POST':
        return context
    else:
        item_form = ItemForm(prefix='item')
        context['item_form'] = item_form
        return context

def post(self, request, *args, **kwargs):
    order_form = OrderForm(request.POST)
    item_form = ItemForm(request.POST, prefix='item')

    if order_form.is_valid() and item_form.is_valid():
        return self.form_valid(order_form)
    else:
        context = self.get_context_data()
        context['item_form'] = item_form
        return render(self.request, 'form.html', context)

Another alternative:

def get_context_data(self, **kwargs):
    context = super(OrderAdd, self).get_context_data(**kwargs)
    if self.request.method == 'POST':
        item_form = ItemForm(self.request.POST, prefix='item')
    else:
        item_form = ItemForm(prefix='item')
    context['item_form'] = item_form
    return context

def post(self, request, *args, **kwargs):
    order_form = OrderForm(request.POST)
    context = self.get_context_data()
    item_form = context['item_form']

    if order_form.is_valid() and item_form.is_valid():
        return self.form_valid(order_form)
    else:
        return render(self.request, 'form.html', context)


来源:https://stackoverflow.com/questions/16422529/using-multiple-modelforms-with-class-based-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!