From 6b4834952dcce0db5cbc1534635c00ff8573a6d8 Mon Sep 17 00:00:00 2001 From: Anssi Kääriäinen Date: Thu, 29 Nov 2012 12:10:31 +0200 Subject: Fixed #16649 -- Refactored save_base logic Model.save() will use UPDATE - if not updated - INSERT instead of SELECT - if found UPDATE else INSERT. This should save a query when updating, but will cost a little when inserting model with PK set. Also fixed #17341 -- made sure .save() commits transactions only after the whole model has been saved. This wasn't the case in model inheritance situations. The save_base implementation was refactored into multiple methods. A typical chain for inherited save is: save_base() _save_parents(self) for each parent: _save_parents(parent) _save_table(parent) _save_table(self) --- docs/ref/models/instances.txt | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'docs/ref') diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index 92071b8d3f..9f583c42ac 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -292,12 +292,13 @@ follows this algorithm: * If the object's primary key attribute is set to a value that evaluates to ``True`` (i.e., a value other than ``None`` or the empty string), Django - executes a ``SELECT`` query to determine whether a record with the given - primary key already exists. -* If the record with the given primary key does already exist, Django - executes an ``UPDATE`` query. -* If the object's primary key attribute is *not* set, or if it's set but a - record doesn't exist, Django executes an ``INSERT``. + executes an ``UPDATE``. +* If the object's primary key attribute is *not* set or if the ``UPDATE`` + didn't update anything, Django executes an ``INSERT``. + +.. versionchanged:: 1.6 + Previously Django used ``SELECT`` - if not found ``INSERT`` else ``UPDATE`` + algorithm. The old algorithm resulted in one more query in ``UPDATE`` case. The one gotcha here is that you should be careful not to specify a primary-key value explicitly when saving new objects, if you cannot guarantee the -- cgit v1.3