diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2018-08-21 07:02:03 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-08-21 10:02:03 -0400 |
| commit | ef87b38ef7b07a5a9b4ee424a04a9811836dee39 (patch) | |
| tree | f03c961e0dc702bb97cec2e92ecb520e9f20f3c0 | |
| parent | cdc6da395aa602d772bd376513121fb2b674bda1 (diff) | |
Fixed #29696 -- Prevented BaseModelFormSet.initial_form_count()'s from treating data={} as unbound.
| -rw-r--r-- | django/forms/models.py | 2 | ||||
| -rw-r--r-- | tests/model_formsets/tests.py | 8 |
2 files changed, 8 insertions, 2 deletions
diff --git a/django/forms/models.py b/django/forms/models.py index aa35ef5f92..7648d97da4 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -570,7 +570,7 @@ class BaseModelFormSet(BaseFormSet): def initial_form_count(self): """Return the number of forms that are required in this FormSet.""" - if not (self.data or self.files): + if not self.is_bound: return len(self.get_queryset()) return super().initial_form_count() diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py index d823a78ae8..097fd32f6a 100644 --- a/tests/model_formsets/tests.py +++ b/tests/model_formsets/tests.py @@ -4,7 +4,7 @@ from datetime import date from decimal import Decimal from django import forms -from django.core.exceptions import ImproperlyConfigured +from django.core.exceptions import ImproperlyConfigured, ValidationError from django.db import models from django.forms.models import ( BaseModelFormSet, _get_foreign_key, inlineformset_factory, @@ -1741,6 +1741,12 @@ class ModelFormsetTest(TestCase): [{'id': ['Select a valid choice. That choice is not one of the available choices.']}], ) + def test_initial_form_count_empty_data_raises_validation_error(self): + AuthorFormSet = modelformset_factory(Author, fields='__all__') + msg = 'ManagementForm data is missing or has been tampered with' + with self.assertRaisesMessage(ValidationError, msg): + AuthorFormSet({}).initial_form_count() + class TestModelFormsetOverridesTroughFormMeta(TestCase): def test_modelformset_factory_widgets(self): |
