Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Multiple ModelForms with Class-Based Views
    primarykey
    data
    text
    <p>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. </p> <p>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?</p> <pre><code>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 </code></pre> <p>Here's my CreateView in it's current form, where I'm currently stuck.</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload