Opened 18 years ago

Closed 17 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: dev
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: no UI/UX: no

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 (2)

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

Download all attachments as: .zip

Change History (12)

comment:1 by florent@…, 18 years ago

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.

comment:2 by anonymous, 17 years ago

Cc: ferringb@… added

comment:3 by Chris Beaven, 17 years ago

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 by James Bennett, 17 years ago

Keywords: qs-rf added

#5417 was a duplicate.

by Deepak <deep.thukral@…>, 17 years ago

Attachment: mult_update.diff added

comment:5 by Deepak <deep.thukral@…>, 17 years ago

Has patch: set
Needs tests: set

Needs intensive testing.

by Jonathan Buchanan, 17 years ago

Attachment: update.diff added

Implements an update method using queries

comment:6 by Jonathan Buchanan, 17 years ago

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 by Malcolm Tredinnick, 17 years ago

(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 by Malcolm Tredinnick, 17 years ago

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

comment:9 by Malcolm Tredinnick, 17 years ago

Triage Stage: Design decision neededAccepted

comment:10 by Malcolm Tredinnick, 17 years ago

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