From e459b0f5a0b2bfbc2ac45b3e7f21047ec9e4f345 Mon Sep 17 00:00:00 2001 From: Mike Lissner Date: Wed, 2 Feb 2022 21:58:14 -0800 Subject: [PATCH] Improved example of using a custom queryset in Model formsets docs. --- docs/topics/forms/modelforms.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index 3045cefd9e..ad0ab35231 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -1069,16 +1069,17 @@ formset:: def manage_authors(request): AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) + queryset = Author.objects.filter(name__startswith='O') if request.method == "POST": formset = AuthorFormSet( request.POST, request.FILES, - queryset=Author.objects.filter(name__startswith='O'), + queryset=queryset, ) if formset.is_valid(): formset.save() # Do something. else: - formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O')) + formset = AuthorFormSet(queryset=queryset) return render(request, 'manage_authors.html', {'formset': formset}) Note that we pass the ``queryset`` argument in both the ``POST`` and ``GET``