#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.