Django

Code

Ticket #7447 (closed: fixed)

Opened 6 months ago

Last modified 5 months ago

QuerySet.update() bypasses custom Model.save() methods

Reported by: emulbreh Assigned to: nobody
Milestone: 1.0 Component: Database layer (models, ORM)
Version: SVN Keywords: qsrf-cleanup
Cc: Triage Stage: Design decision needed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

This is symmetrical to #6915 (and therefore should be handled simmilar). Options:

  • iterate the queryset, update and save() each instance individually. Because this would also work for sliced querysets #7298 would deserve a better fix then.
  • assert not self.has_custom_save(), "Cannot update a query for a model with a custom save() method.". This will probably break ManyRelated managers.
  • only add a big warning to QuerySet.update() and Model.save() doc. This doesn't help at all to solve the issue.

+1 for the first option.

Attachments

Change History

06/13/08 18:03:02 changed by emulbreh

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Basically the same issue: How will QuerySet.update() work with model validation (#6845)?

06/15/08 09:33:50 changed by emulbreh

  • stage changed from Unreviewed to Design decision needed.

06/16/08 12:10:45 changed by jacob

  • milestone set to 1.0.

07/08/08 13:08:51 changed by anonymous

If update() is changed to iterate over every model, it's going to be much, much slower for querysets with thousands or more records.

Is there a way to check for a custom save method in the model, and only iterate if a custom save exists?

07/08/08 13:11:37 changed by anonymous

Alternatively, add a new method, similar to update (let's call it "queryset.save_data()" for example), and guarantee that that iterates no matter what, and leave the update() method alone.

Then, clearly document the fact that update() bypasses custom saves and is fast, save_data() uses custom saves but is slow.

07/08/08 15:44:02 changed by emulbreh

It should be possible to detect a custom update() method. I attempted to the same for delete() in my patch for #6915. But it's pretty much untested and therefore probably broken ..

07/11/08 05:01:46 changed by mtredinnick

Changing the current behaviour isn't going to happen. The update() method is intended to be a bulk SQL operation. If you want to call the save() method on every item in the queryset, loop over the queryset. It's two lines of code. I will commit an update to the documentation for update() to mention that it's a direct SQL operation.

(This isn't quite the same as delete() for internal implementation reasons, so #6915 is still open for that reason.)

07/11/08 05:03:04 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7884]) Documented that the update() method on querysets is a direct SQL call, not the same as looping over the queryset and calling save() on each item (which is less efficient). Fixed #7447.


Add/Change #7447 (QuerySet.update() bypasses custom Model.save() methods)




Change Properties
Action