Opened 18 years ago
Closed 17 years ago
#4260 closed (fixed)
add an update method to QuerySet
Reported by: | 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)
Change History (12)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
Cc: | added |
---|
comment:3 by , 17 years ago
Triage Stage: | Unreviewed → 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).
by , 17 years ago
Attachment: | mult_update.diff added |
---|
comment:6 by , 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 , 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 , 17 years ago
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:9 by , 17 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:10 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(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
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.