From e87f57fdb8dcdabc452bd15abd015bf6c9b1f7a8 Mon Sep 17 00:00:00 2001 From: vgolubev Date: Sun, 1 Aug 2021 23:33:12 +0300 Subject: Fixed #26142 -- Allowed model formsets to prevent new object creation. Thanks Jacob Walls, David Smith, and Mariusz Felisiak for reviews. Co-authored-by: parth --- docs/topics/forms/modelforms.txt | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'docs/topics/forms') diff --git a/docs/topics/forms/modelforms.txt b/docs/topics/forms/modelforms.txt index cf79fc69c4..3045cefd9e 100644 --- a/docs/topics/forms/modelforms.txt +++ b/docs/topics/forms/modelforms.txt @@ -953,8 +953,8 @@ extra forms displayed. Also, ``extra=0`` doesn't prevent creation of new model instances as you can :ref:`add additional forms with JavaScript ` -or send additional POST data. Formsets :ticket:`don't yet provide functionality -<26142>` for an "edit only" view that prevents creation of new instances. +or send additional POST data. See :ref:`model-formsets-edit-only` on how to do +this. If the value of ``max_num`` is greater than the number of existing related objects, up to ``extra`` additional blank forms will be added to the formset, @@ -972,6 +972,25 @@ so long as the total number of forms does not exceed ``max_num``:: A ``max_num`` value of ``None`` (the default) puts a high limit on the number of forms displayed (1000). In practice this is equivalent to no limit. +.. _model-formsets-edit-only: + +Preventing new objects creation +------------------------------- + +.. versionadded:: 4.1 + +Using the ``edit_only`` parameter, you can prevent creation of any new +objects:: + + >>> AuthorFormSet = modelformset_factory( + ... Author, + ... fields=('name', 'title'), + ... edit_only=True, + ... ) + +Here, the formset will only edit existing ``Author`` instances. No other +objects will be created or edited. + Using a model formset in a view ------------------------------- -- cgit v1.3