Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion django/forms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@
save_as_new=False,
prefix=None,
queryset=None,
auto_id="id_%s",
**kwargs,
):
if instance is None:
Expand All @@ -1126,7 +1127,7 @@
else:
qs = queryset.none()
self.unique_fields = {self.fk.name}
super().__init__(data, files, prefix=prefix, queryset=qs, **kwargs)
super().__init__(data, files, auto_id=auto_id, prefix=prefix, queryset=qs, **kwargs)

Check failure on line 1130 in django/forms/models.py

View workflow job for this annotation

GitHub Actions / flake8

line too long (92 > 88 characters)

# Add the generated field to form._meta.fields if it's defined to make
# sure validation isn't skipped on that field.
Expand Down
25 changes: 25 additions & 0 deletions tests/model_formsets/test_auto_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.test import TestCase
from django.forms.models import inlineformset_factory
from .models import Author, Book

class InlineFormsetAutoIdTest(TestCase):

Check failure on line 5 in tests/model_formsets/test_auto_id.py

View workflow job for this annotation

GitHub Actions / flake8

expected 2 blank lines, found 1
def test_inline_formset_auto_id(self):
"""Test that BaseInlineFormSet accepts the auto_id parameter."""
# Create an inline formset with a custom auto_id
AuthorBooksFormSet = inlineformset_factory(
Author, Book, can_delete=False, extra=1, fields="__all__"
)
author = Author.objects.create(name="Test Author")

# Test with default auto_id
formset = AuthorBooksFormSet(instance=author)
self.assertEqual(formset.auto_id, "id_%s")

# Test with custom auto_id
custom_auto_id = "custom_%s"
formset = AuthorBooksFormSet(instance=author, auto_id=custom_auto_id)
self.assertEqual(formset.auto_id, custom_auto_id)

# Verify that the auto_id is used in the rendered form
form_html = formset.forms[0].as_p()
self.assertIn(f'id="custom_book_set-0-title"', form_html)

Check failure on line 25 in tests/model_formsets/test_auto_id.py

View workflow job for this annotation

GitHub Actions / flake8

f-string is missing placeholders
Loading