Note that there are some explanatory texts on larger screens.

plurals
  1. POInner FormSet requires manual refreshing from request
    primarykey
    data
    text
    <p>I noticed weird behaviour of FormSet which is nested inside another form.<br> Sample app: </p> <pre><code>#### forms: #### class BookForm(forms.Form): title = forms.CharField() BookFormSet = formset_factory(BookForm, extra=3) class PublisherForm(forms.Form): name = forms.CharField() books = BookFormSet(prefix='books') </code></pre> <p>Class-based view for displaying forms:</p> <pre><code>#### views: #### class PublisherCreateView(FormView): template_name = 'library/create.html' form_class = PublisherForm def form_valid(self, publisherForm): # workaround: somehow publisherForm's inner list need to be restored from POST request: # otherwise it'll be empty FormSet as if it was constructed using BookFormSet(prefix='books') books = BookFormSet(self.request.POST, self.request.FILES, prefix='books') publisherForm.books = books do_sth_fancy_dancy_with(publisherForm) return super(PublisherCreateView, self).form_valid(publisherForm) </code></pre> <p>An the template used for displaying PublisherForm: </p> <pre><code>#### template: #### &lt;form action="." method="post"&gt;{% csrf_token %} &lt;div class="section"&gt; {{ form.as_p }} &lt;/div&gt; &lt;h2&gt;Books&lt;/h2&gt; &lt;div class="books"&gt; {{ form.books.as_p }} &lt;p&gt;&lt;input type="button" id="add-row" value="Add another book"/&gt;&lt;/p&gt; &lt;/div&gt; &lt;input type="submit" value="Save"/&gt; &lt;/form&gt; </code></pre> <p>If I omit first 2 lines in form_valid the list is untouched by its modification in a browser.<br> A nice explanation why is this happening, or maybe amend of my code would be more than appreciated</p>
    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