﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14917	"Error in the sample code under ""Using an inline formset in a view"""	baddox	nobody	"http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-an-inline-formset-in-a-view

I believe the sample code under the above linked paragraph should have the ""formset = BookInlineFormSet(instance=author)"" line executed regardless of the request method. Following the example as it is now, the page reload after filling in one of the extra forms and saving will have one fewer than the specified number of extra forms. Similarly, the page reload after marking a bound form for deletion and saving will still have that just-deleted form present in the formset. Moving the formset variable assignment outside of and after the ""if request.method == ""POST"":"" block (and thus getting rid of the ""else:"" block) fixes this problem. The entire corrected sample code would be as such:

{{{
def manage_books(request, author_id):
    author = Author.objects.get(pk=author_id)
    BookInlineFormSet = inlineformset_factory(Author, Book)
    if request.method == ""POST"":
        formset = BookInlineFormSet(request.POST, request.FILES, instance=author)
        if formset.is_valid():
            formset.save()
            # Do something.
    formset = BookInlineFormSet(instance=author)
    return render_to_response(""manage_books.html"", {
        ""formset"": formset,
    })
}}}
"	Bug	closed	Documentation	1.2	Normal	fixed			Accepted	0	0	0	0	0	0
