Django

Code

Ticket #4260 (closed: fixed)

Opened 1 year ago

Last modified 3 weeks ago

add an update method to QuerySet

Reported by: Gary Wilson <gary.wilson@gmail.com> Assigned to: nobody
Component: Database wrapper Version: SVN
Keywords: qs-rf-fixed Cc: ferringb@gmail.com
Triage Stage: Accepted Has patch: 1
Needs documentation: 1 Needs tests: 1
Patch needs improvement: 0

Description

One should be able to update multiple records at once with something like

User.objects.all().update(is_active=True)

instead of having to update one at a time with something like

for user in User.objects.all():
    user.is_active = True
    user.save()

Attachments

mult_update.diff (1.0 kB) - added by Deepak <deep.thukral@gmail.com> on 09/17/07 03:04:58.
update.diff (6.9 kB) - added by insin on 01/18/08 11:11:45.
Implements an update method using queries

Change History

06/15/07 11:27:22 changed by florent@secondweb.fr

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

Hi,

I am very interested in such a feature, as I use Django for a server delivering REST web services, with important performance issues: I can not select all items then loop in python to perform individual update queries.

I was wondering how many people were interested in this feature, so that we can help coding it. It does not seem to difficult as such an update sql command really looks like a select, but as I am not an experienced Django developer, I would probably need some directions. Anyone else interested? Is it the right place to ask or not?

Florent.

07/11/07 13:58:42 changed by anonymous

  • cc set to ferringb@gmail.com.

08/16/07 18:26:21 changed by SmileyChris

  • stage changed from Unreviewed to Design decision needed.

I did some work into this a while back but it never eventuated to much.

One gotcha: SQLite can't use joins in its UPDATE statement (so I guess you'd have to get the filtered PKs and do a WHERE against that for SQLite).

09/16/07 17:10:15 changed by ubernostrum

  • keywords set to qs-rf.

#5417 was a duplicate.

09/17/07 03:04:58 changed by Deepak <deep.thukral@gmail.com>

  • attachment mult_update.diff added.

09/17/07 03:05:51 changed by Deepak <deep.thukral@gmail.com>

  • has_patch set to 1.
  • needs_tests set to 1.

Needs intensive testing.

01/18/08 11:11:45 changed by insin

  • attachment update.diff added.

Implements an update method using queries

01/18/08 11:15:15 changed by insin

  • needs_docs set to 1.

I can only see updates for single objects in the qs-rf branch at the moment and I wanted to learn a bit more about the current query internals anyway, so here's a patch which adds an update method (which executes SQL instead of looping over objects), implements add, subtract (which take an int value) and sql (which takes a query or a two-tuple of query, parameters) double-underscore style "lookups" and takes care of the SQLite gotcha.

Only tested lightly with SQLite so far.

01/28/08 08:27:54 changed by mtredinnick

(In [7043]) queryset-refactor: Added an update method to QuerySets?, since it's needed for moving SQL out of the core code. Only direct fields and foreign keys can be updated in this fashion, since multi-table updates are very non-portable.

This also cleans up the API for the UpdateQuery? class a bit. Still need to change DeleteQuery? to work similarly, I suspect.

Refs #4260.

01/28/08 08:39:54 changed by mtredinnick

  • keywords changed from qs-rf to qs-rf-fixed.

01/28/08 08:40:01 changed by mtredinnick

  • stage changed from Design decision needed to Accepted.

04/26/08 21:50:16 changed by mtredinnick

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

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658


Add/Change #4260 (add an update method to QuerySet)




Change Properties
Action