1
0
mirror of https://github.com/django/django.git synced 2025-08-27 12:19:12 +00:00

Fixed #36251 -- Avoided mutating form Meta.fields in BaseInlineFormSet.

Signed-off-by: SaJH <wogur981208@gmail.com>
This commit is contained in:
SaJH 2025-08-18 23:40:54 +09:00 committed by Sarah Boyce
parent 165ad74c57
commit 3ba24c18e7
2 changed files with 4 additions and 3 deletions

View File

@ -1137,7 +1137,6 @@ class BaseInlineFormSet(BaseModelFormSet):
# Add the inline foreign key field to form._meta.fields if it's defined # Add the inline foreign key field to form._meta.fields if it's defined
# to make sure validation isn't skipped on that field. # to make sure validation isn't skipped on that field.
if self.form._meta.fields and self.fk.name not in self.form._meta.fields: if self.form._meta.fields and self.fk.name not in self.form._meta.fields:
if isinstance(self.form._meta.fields, tuple):
self.form._meta.fields = list(self.form._meta.fields) self.form._meta.fields = list(self.form._meta.fields)
self.form._meta.fields.append(self.fk.name) self.form._meta.fields.append(self.fk.name)

View File

@ -1687,9 +1687,10 @@ class ModelFormsetTest(TestCase):
class Meta: class Meta:
model = Book model = Book
fields = ("title",) fields = ["title"]
BookFormSet = inlineformset_factory(Author, Book, form=BookForm) BookFormSet = inlineformset_factory(Author, Book, form=BookForm)
self.assertEqual(BookForm.Meta.fields, ["title"])
data = { data = {
"book_set-TOTAL_FORMS": "3", "book_set-TOTAL_FORMS": "3",
"book_set-INITIAL_FORMS": "0", "book_set-INITIAL_FORMS": "0",
@ -1700,6 +1701,7 @@ class ModelFormsetTest(TestCase):
} }
author = Author.objects.create(name="test") author = Author.objects.create(name="test")
formset = BookFormSet(data, instance=author) formset = BookFormSet(data, instance=author)
self.assertEqual(BookForm.Meta.fields, ["title"])
self.assertEqual( self.assertEqual(
formset.errors, formset.errors,
[{}, {"__all__": ["Please correct the duplicate values below."]}, {}], [{}, {"__all__": ["Please correct the duplicate values below."]}, {}],