Fixed #29696 -- Prevented BaseModelFormSet.initial_form_count()'s from treating data={} as unbound.

This commit is contained in:
Jon Dufresne 2018-08-21 07:02:03 -07:00 committed by Tim Graham
parent cdc6da395a
commit ef87b38ef7
2 changed files with 8 additions and 2 deletions

View File

@ -570,7 +570,7 @@ class BaseModelFormSet(BaseFormSet):
def initial_form_count(self): def initial_form_count(self):
"""Return the number of forms that are required in this FormSet.""" """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 len(self.get_queryset())
return super().initial_form_count() return super().initial_form_count()

View File

@ -4,7 +4,7 @@ from datetime import date
from decimal import Decimal from decimal import Decimal
from django import forms from django import forms
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db import models from django.db import models
from django.forms.models import ( from django.forms.models import (
BaseModelFormSet, _get_foreign_key, inlineformset_factory, 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.']}], [{'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): class TestModelFormsetOverridesTroughFormMeta(TestCase):
def test_modelformset_factory_widgets(self): def test_modelformset_factory_widgets(self):