[Django] #37145: ModelFormSet breaks when changing prefix format

13 views
Skip to first unread message

Django

unread,
Jun 5, 2026, 10:19:08 AMJun 5
to django-...@googlegroups.com
#37145: ModelFormSet breaks when changing prefix format
-----------------------------+--------------------------------------
Reporter: JortRoelofs | Type: Bug
Status: new | Component: Forms
Version: 6.0 | Severity: Normal
Keywords: formset | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
I've been trying to change the way the widget prefixes are formatted to
make it work nicely with some JavaScript libraries. Here I ran into the
following bug.

{{{class BookForm(forms.ModelForm):}}}
{{{ class Meta:}}}
{{{ ...}}}

{{{ def add_prefix(self, field_name)}}}
{{{ # Here I use a period as separator instead of a dash.}}}
{{{ return f"{self.prefix}.{field_name}" if self.prefix else
field_name}}}

{{{BookFormSet = modelformset_factory(Book, form=BookForm)}}}


So far so good, this renders the way I expect it to. However, form
submission doesn't work, as the check for the primary key is hardcoded to
use a dash as separator
[https://github.com/django/django/blob/a2348c85fc6c20087935c74cd99340dd4ef2dcdc/django/forms/models.py#L739
here]. When looking for a primary key, it constructs a key with pk_key =
"%s-%s" % (self.add_prefix(i), self.model._meta.pk.name), but this is
inconsistent when the form itself has defined a different way to construct
prefixes.
--
Ticket URL: <https://code.djangoproject.com/ticket/37145>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 9, 2026, 5:10:24 PMJun 9
to django-...@googlegroups.com
#37145: ModelFormSet breaks when changing prefix format
-------------------------------------+-------------------------------------
Reporter: Jort Roelofs | Owner: (none)
Type: Bug | Status: new
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: ModelFormSet, | Triage Stage: Accepted
add_prefix |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* cc: Clifford Gama (added)
* keywords: formset => ModelFormSet, add_prefix
* stage: Unreviewed => Accepted
* version: 6.0 => dev

Comment:

Thanks for the report! I verified that overriding `add_prefix` on FormSet
works, so this bug is only scoped to `ModelFormSet`. (We may also need to
add tests for `FormSet` as well.)

Would you like to prepare a patch?
--
Ticket URL: <https://code.djangoproject.com/ticket/37145#comment:1>

Django

unread,
Jun 10, 2026, 11:06:13 AMJun 10
to django-...@googlegroups.com
#37145: ModelFormSet breaks when changing prefix format
-------------------------------------+-------------------------------------
Reporter: Jort Roelofs | Owner: diaxoaine
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: ModelFormSet, | Triage Stage: Accepted
add_prefix |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by diaxoaine):

* owner: (none) => diaxoaine
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/37145#comment:2>

Django

unread,
Jun 14, 2026, 1:06:12 AM (13 days ago) Jun 14
to django-...@googlegroups.com
#37145: ModelFormSet breaks when changing prefix format
-------------------------------------+-------------------------------------
Reporter: Jort Roelofs | Owner: hi
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: ModelFormSet, | Triage Stage: Accepted
add_prefix |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by hi):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37145#comment:3>

Django

unread,
Jun 16, 2026, 4:17:20 PM (10 days ago) Jun 16
to django-...@googlegroups.com
#37145: ModelFormSet breaks when changing prefix format
-------------------------------------+-------------------------------------
Reporter: Jort Roelofs | Owner: diaxoaine
Type: Bug | Status: assigned
Component: Forms | Version: dev
Severity: Normal | Resolution:
Keywords: ModelFormSet, | Triage Stage: Accepted
add_prefix |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/37145#comment:4>
Reply all
Reply to author
Forward
0 new messages