[4.0.x] Improved example of using a custom queryset in Model formsets docs.

Backport of e459b0f5a0 from main
This commit is contained in:
Mike Lissner 2022-02-02 21:58:14 -08:00 committed by Mariusz Felisiak
parent efe72cf128
commit 62fe7e08d6
1 changed files with 3 additions and 2 deletions

View File

@ -1050,16 +1050,17 @@ formset::
def manage_authors(request): def manage_authors(request):
AuthorFormSet = modelformset_factory(Author, fields=('name', 'title')) AuthorFormSet = modelformset_factory(Author, fields=('name', 'title'))
queryset = Author.objects.filter(name__startswith='O')
if request.method == "POST": if request.method == "POST":
formset = AuthorFormSet( formset = AuthorFormSet(
request.POST, request.FILES, request.POST, request.FILES,
queryset=Author.objects.filter(name__startswith='O'), queryset=queryset,
) )
if formset.is_valid(): if formset.is_valid():
formset.save() formset.save()
# Do something. # Do something.
else: else:
formset = AuthorFormSet(queryset=Author.objects.filter(name__startswith='O')) formset = AuthorFormSet(queryset=queryset)
return render(request, 'manage_authors.html', {'formset': formset}) return render(request, 'manage_authors.html', {'formset': formset})
Note that we pass the ``queryset`` argument in both the ``POST`` and ``GET`` Note that we pass the ``queryset`` argument in both the ``POST`` and ``GET``