Opened 10 years ago

Closed 9 years ago

#4260 closed (fixed)

add an update method to QuerySet

Reported by: Gary Wilson <gary.wilson@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: qs-rf-fixed
Cc: ferringb@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:


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


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

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

Attachments (2)

mult_update.diff (1.0 KB) - added by Deepak <deep.thukral@…> 10 years ago.
update.diff (6.9 KB) - added by Jonathan Buchanan 9 years ago.
Implements an update method using queries

Download all attachments as: .zip

Change History (12)

comment:1 Changed 10 years ago by florent@…


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?


comment:2 Changed 10 years ago by anonymous

Cc: ferringb@… added

comment:3 Changed 10 years ago by Chris Beaven

Triage Stage: UnreviewedDesign 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).

comment:4 Changed 10 years ago by James Bennett

Keywords: qs-rf added

#5417 was a duplicate.

Changed 10 years ago by Deepak <deep.thukral@…>

Attachment: mult_update.diff added

comment:5 Changed 10 years ago by Deepak <deep.thukral@…>

Has patch: set
Needs tests: set

Needs intensive testing.

Changed 9 years ago by Jonathan Buchanan

Attachment: update.diff added

Implements an update method using queries

comment:6 Changed 9 years ago by Jonathan Buchanan

Needs documentation: set

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.

comment:7 Changed 9 years ago by Malcolm Tredinnick

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

comment:8 Changed 9 years ago by Malcolm Tredinnick

Keywords: qs-rf-fixed added; qs-rf removed

comment:9 Changed 9 years ago by Malcolm Tredinnick

Triage Stage: Design decision neededAccepted

comment:10 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(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

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