Fixed #24041 -- Documented effect of changing a model instance's primary key.
This commit is contained in:
parent
3daa9d60be
commit
4ccdf6e57f
|
@ -278,8 +278,13 @@ don't need to set ``primary_key=True`` on any of your fields unless you want to
|
||||||
override the default primary-key behavior. For more, see
|
override the default primary-key behavior. For more, see
|
||||||
:ref:`automatic-primary-key-fields`.
|
:ref:`automatic-primary-key-fields`.
|
||||||
|
|
||||||
``primary_key=True`` implies :attr:`null=False <Field.null>` and :attr:`unique=True <Field.unique>`.
|
``primary_key=True`` implies :attr:`null=False <Field.null>` and
|
||||||
Only one primary key is allowed on an object.
|
:attr:`unique=True <Field.unique>`. Only one primary key is allowed on an
|
||||||
|
object.
|
||||||
|
|
||||||
|
The primary key field is read-only. If you change the value of the primary
|
||||||
|
key on an existing object and then save it, a new object will be created
|
||||||
|
alongside the old one.
|
||||||
|
|
||||||
``unique``
|
``unique``
|
||||||
----------
|
----------
|
||||||
|
|
|
@ -213,6 +213,23 @@ ones:
|
||||||
unless you want to override the default primary-key behavior. For more,
|
unless you want to override the default primary-key behavior. For more,
|
||||||
see :ref:`automatic-primary-key-fields`.
|
see :ref:`automatic-primary-key-fields`.
|
||||||
|
|
||||||
|
The primary key field is read-only. If you change the value of the primary
|
||||||
|
key on an existing object and then save it, a new object will be created
|
||||||
|
alongside the old one. For example::
|
||||||
|
|
||||||
|
from django.db import models
|
||||||
|
|
||||||
|
class Fruit(models.Model):
|
||||||
|
name = models.CharField(max_length=100, primary_key=True)
|
||||||
|
|
||||||
|
.. code-block:: pycon
|
||||||
|
|
||||||
|
>>> fruit = Fruit.objects.create(name='Apple')
|
||||||
|
>>> fruit.name = 'Pear'
|
||||||
|
>>> fruit.save()
|
||||||
|
>>> Fruit.objects.values_list('name', flat=True)
|
||||||
|
['Apple', 'Pear']
|
||||||
|
|
||||||
:attr:`~Field.unique`
|
:attr:`~Field.unique`
|
||||||
If ``True``, this field must be unique throughout the table.
|
If ``True``, this field must be unique throughout the table.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue