Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33534 closed Bug (invalid)

Formset form with initial values doesnt save

Reported by: saschahofmann Owned by: nobody
Component: Forms Version: 4.0
Severity: Normal Keywords: formset initial save
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Calling formset.save() on a formset with an extra_form that only hasn't changed any of the initial values doesn't save.

I believe the cause is in https://github.com/django/django/blob/main/django/forms/models.py#L942. form.has_changed() checks whether data is different compared to initial values. This behaviour is incosistent with form.save() e.g. you could call something like formset.extra_forms[0].save() and this would save the form.

It seems reasonable to me to simple remove that line! Why would a new item not be saved on Submit?

Happy to submit the according PR.

Change History (3)

comment:1 by Tim Graham, 3 years ago

Resolution: invalid
Status: newclosed

Formsets can have extra forms that may not be changed. In that case, they shouldn't be treated as new data that the user submitted.

in reply to:  1 comment:2 by saschahofmann, 3 years ago

I guess that makes sense if an extra_form is invalid/not filled out but if I submit a correctly filled out form I would expect it to be saved or not? Maybe it'd be nice to be able to configure that behaviour? Right now, I am going to overwrite the save_new_objects function and remove the line.

Replying to Tim Graham:

Formsets can have extra forms that may not be changed. In that case, they shouldn't be treated as new data that the user submitted.

comment:3 by Tim Graham, 3 years ago

There's a discussion in #24541 which documented the behavior. I don't know that the use case is common enough to warrant a new option. A set of completely prefilled forms that require no input from the user seems quite niche.

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