From de94d0cb9381bcab02b7be0de1d8ac577bcfab92 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 28 Jan 2008 14:27:53 +0000 Subject: 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. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7043 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/db-api.txt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'docs') diff --git a/docs/db-api.txt b/docs/db-api.txt index 342f635219..c37ebd083a 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -1972,6 +1972,34 @@ complete query set:: Entry.objects.all().delete() +Updating multiple objects at once +================================= + +**New in Django development version** + +Sometimes you want to set a field to a particular value for all the objects in +a queryset. You can do this with the ``update()`` method. For example:: + + # Update all the headlings to the same value. + Entry.objects.all().update(headline='Everything is the same') + +You can only set non-relation fields and ``ForeignKey`` fields using this +method and the value you set the field to must be a normal Python value (you +can't set a field to be equal to some other field at the moment). + +To update ``ForeignKey`` fields, set the new value to be the new model +instance you want to point to. Example:: + + b = Blog.objects.get(pk=1) + # Make all entries belong to this blog. + Entry.objects.all().update(blog=b) + +The ``update()`` method is applied instantly and doesn't return anything +(similar to ``delete()``). The only restriction on the queryset that is +updated is that it can only access one database table, the model's main +table. So don't try to filter based on related fields or anything like that; +it won't work. + Extra instance methods ====================== -- cgit v1.3