Opened 7 years ago

Closed 6 years ago

Last modified 6 years ago

#8672 closed (wontfix)

ModelForm.save() should implement force_insert and force_update, like Model.save()

Reported by: Richard Davies <richard.davies@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: richard.davies@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

[8267] added the ability to force an SQL insert (or force an update) via a model's save() method.

The same force_insert and force_update flags should be supported in a modelform's save() method.

These flags enable better control over some edge cases - e.g. force_insert will prevent overwriting existing data if the same primary key has accidentally been specified.

Attachments (2)

flags-for-modelform-save.diff (4.2 KB) - added by Richard Davies <richard.davies@…> 7 years ago.
one-more.diff (4.8 KB) - added by Richard Davies <richard.davies@…> 7 years ago.
Additional to previous patch

Download all attachments as: .zip

Change History (11)

Changed 7 years ago by Richard Davies <richard.davies@…>

Changed 7 years ago by Richard Davies <richard.davies@…>

Additional to previous patch

comment:1 Changed 7 years ago by Richard Davies <richard.davies@…>

  • Has patch set
  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

The patch one-more.diff is the most recent, and contains all required changes.

I notice that the two helper functions make_model_save and make_instance_save are not used anywhere in Django, so are probably legacy which could be removed. I haven't done this here - just patched them along with the rest.

comment:2 Changed 7 years ago by Richard Davies <richard.davies@…>

  • Component changed from Forms to Database wrapper

comment:3 Changed 7 years ago by Alex

  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 Changed 7 years ago by mtredinnick

  • milestone changed from 1.0 to post-1.0

comment:5 Changed 6 years ago by dc

After this patch ModelForm.save() signature will be different from Form.save(). Is this ok?
Desired effect can already be achieved with

model = model_form.save(commit=False)
model.save(force_insert=True)

comment:6 Changed 6 years ago by mtredinnick

(In [9539]) Added a short note to the modelforms documentation linking between
save(commit=False) and using force_insert or force_update on the Model.save()
call. Refs #8672.

comment:7 Changed 6 years ago by mtredinnick

  • Resolution set to wontfix
  • Status changed from new to closed

The consensus amongst the maintainers is that complicating the API a bit by adding two extra parameters isn't worth it in this case. I've added a note to the documentation to remind people about commit=False in these circumstances.

comment:8 Changed 6 years ago by mtredinnick

(In [9541]) [1.0.X] Added a short note to the modelforms documentation linking between
save(commit=False) and using force_insert or force_update on the Model.save()
call. Refs #8672.

Backport of r9539 from trunk.

comment:9 Changed 6 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

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