diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2012-11-29 12:10:31 +0200 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-03-14 11:01:47 +0200 |
| commit | 6b4834952dcce0db5cbc1534635c00ff8573a6d8 (patch) | |
| tree | fdf79be9ab8152086cf7401b949720ca1597c35e /docs | |
| parent | 8a2f5300b21c3c20a9fc6829d9fcadb023cba4a8 (diff) | |
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)
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/instances.txt | 13 | ||||
| -rw-r--r-- | docs/releases/1.6.txt | 4 |
2 files changed, 11 insertions, 6 deletions
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 diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 30c3cc5d2c..a44545ddf3 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -150,6 +150,10 @@ Minor features * Generic :class:`~django.contrib.gis.db.models.GeometryField` is now editable with the OpenLayers widget in the admin. +* The :meth:`Model.save() <django.db.models.Model.save()>` will do + ``UPDATE`` - if not updated - ``INSERT`` instead of ``SELECT`` - if not + found ``INSERT`` else ``UPDATE`` in case the model's primary key is set. + Backwards incompatible changes in 1.6 ===================================== |
