Skip to content

Commit

Permalink
Refs #29260 -- Doc'd Model.save() behavior change in Django 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
charettes authored and felixxm committed Dec 12, 2019
1 parent 7536b12 commit 3e8e213
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/ref/models/instances.txt
Expand Up @@ -490,6 +490,12 @@ which returns ``NULL``. In such cases it is possible to revert to the old
algorithm by setting the :attr:`~django.db.models.Options.select_on_save`
option to ``True``.

.. versionchanged:: 3.0

``Model.save()`` no longer attempts to find a row when saving a new
``Model`` instance and a default value for the primary key is provided, and
always executes an ``INSERT``.

.. _ref-models-force-insert:

Forcing an INSERT or UPDATE
Expand Down
20 changes: 20 additions & 0 deletions docs/releases/3.0.txt
Expand Up @@ -372,6 +372,26 @@ Tests
Backwards incompatible changes in 3.0
=====================================

``Model.save()`` when providing a default for the primary key
-------------------------------------------------------------

:meth:`.Model.save` no longer attempts to find a row when saving a new
``Model`` instance and a default value for the primary key is provided, and
always performs a single ``INSERT`` query. In older Django versions,
``Model.save()`` performed either an ``INSERT`` or an ``UPDATE`` based on
whether or not the row exists.

This makes calling ``Model.save()`` while providing a default primary key value
equivalent to passing :ref:`force_insert=True <ref-models-force-insert>` to
model's ``save()``. Attempts to use a new ``Model`` instance to update an
existing row will result in an ``IntegrityError``.

In order to update an existing model for a specific primary key value use the
:meth:`~django.db.models.query.QuerySet.update_or_create` method instead. For
example::

>>> MyModel.objects.update_or_create(pk=existing_pk, defaults={'name': 'new name'})

Database backend API
--------------------

Expand Down

0 comments on commit 3e8e213

Please sign in to comment.