#37145 new Bug

ModelFormSet breaks when changing prefix format

Reported by: JortRoelofs Owned by:
Component: Forms Version: 6.0
Severity: Normal Keywords: formset
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top