mirror of
https://github.com/django/django.git
synced 2025-07-30 08:35:23 +00:00
Fixed #31007 -- Allowed specifying type of auto-created primary keys.
This also changes the default type of auto-created primary keys for new apps and projects to BigAutoField.
This commit is contained in:
parent
b960e4ed72
commit
b5e12d490a
28 changed files with 415 additions and 11 deletions
|
@ -90,6 +90,7 @@ would provide a proper name for the admin::
|
|||
from django.apps import AppConfig
|
||||
|
||||
class RockNRollConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'rock_n_roll'
|
||||
verbose_name = "Rock ’n’ roll"
|
||||
|
||||
|
@ -219,6 +220,16 @@ Configurable attributes
|
|||
|
||||
By default, this attribute isn't set.
|
||||
|
||||
.. attribute:: AppConfig.default_auto_field
|
||||
|
||||
.. versionadded:: 3.2
|
||||
|
||||
The implicit primary key type to add to models within this app. You can
|
||||
use this to keep :class:`~django.db.models.AutoField` as the primary key
|
||||
type for third party applications.
|
||||
|
||||
By default, this is the value of :setting:`DEFAULT_AUTO_FIELD`.
|
||||
|
||||
Read-only attributes
|
||||
--------------------
|
||||
|
||||
|
|
|
@ -378,6 +378,8 @@ Models
|
|||
* **models.W040**: ``<database>`` does not support indexes with non-key
|
||||
columns.
|
||||
* **models.E041**: ``constraints`` refers to the joined field ``<field name>``.
|
||||
* **models.W042**: Auto-created primary key used when not defining a primary
|
||||
key type, by default ``django.db.models.AutoField``.
|
||||
|
||||
Security
|
||||
--------
|
||||
|
|
|
@ -415,9 +415,12 @@ cross-site scripting attack.
|
|||
If ``True``, this field is the primary key for the model.
|
||||
|
||||
If you don't specify ``primary_key=True`` for any field in your model, Django
|
||||
will automatically add an :class:`AutoField` to hold the primary key, so you
|
||||
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
|
||||
will automatically add a field to hold the primary key, so you don't need to
|
||||
set ``primary_key=True`` on any of your fields unless you want to override the
|
||||
default primary-key behavior. The type of auto-created primary key fields can
|
||||
be specified per app in :attr:`AppConfig.default_auto_field
|
||||
<django.apps.AppConfig.default_auto_field>` or globally in the
|
||||
:setting:`DEFAULT_AUTO_FIELD` setting. For more, see
|
||||
:ref:`automatic-primary-key-fields`.
|
||||
|
||||
``primary_key=True`` implies :attr:`null=False <Field.null>` and
|
||||
|
@ -428,6 +431,11 @@ 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.
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
In older versions, auto-created primary key fields were always
|
||||
:class:`AutoField`\s.
|
||||
|
||||
``unique``
|
||||
----------
|
||||
|
||||
|
|
|
@ -1245,6 +1245,17 @@ format has higher precedence and will be applied instead.
|
|||
See also :setting:`NUMBER_GROUPING`, :setting:`THOUSAND_SEPARATOR` and
|
||||
:setting:`USE_THOUSAND_SEPARATOR`.
|
||||
|
||||
.. setting:: DEFAULT_AUTO_FIELD
|
||||
|
||||
``DEFAULT_AUTO_FIELD``
|
||||
----------------------
|
||||
|
||||
.. versionadded:: 3.2
|
||||
|
||||
Default: ``'``:class:`django.db.models.AutoField`\ ``'``
|
||||
|
||||
Default primary key field type to use for models that don't have a field with
|
||||
:attr:`primary_key=True <django.db.models.Field.primary_key>`.
|
||||
|
||||
.. setting:: DEFAULT_CHARSET
|
||||
|
||||
|
|
|
@ -53,6 +53,48 @@ needed. As a consequence, it's deprecated.
|
|||
|
||||
See :ref:`configuring-applications-ref` for full details.
|
||||
|
||||
Customizing type of auto-created primary keys
|
||||
---------------------------------------------
|
||||
|
||||
When defining a model, if no field in a model is defined with
|
||||
:attr:`primary_key=True <django.db.models.Field.primary_key>` an implicit
|
||||
primary key is added. The type of this implicit primary key can now be
|
||||
controlled via the :setting:`DEFAULT_AUTO_FIELD` setting and
|
||||
:attr:`AppConfig.default_auto_field <django.apps.AppConfig.default_auto_field>`
|
||||
attribute. No more needing to override primary keys in all models.
|
||||
|
||||
Maintaining the historical behavior, the default value for
|
||||
:setting:`DEFAULT_AUTO_FIELD` is :class:`~django.db.models.AutoField`. Starting
|
||||
with 3.2 new projects are generated with :setting:`DEFAULT_AUTO_FIELD` set to
|
||||
:class:`~django.db.models.BigAutoField`. Also, new apps are generated with
|
||||
:attr:`AppConfig.default_auto_field <django.apps.AppConfig.default_auto_field>`
|
||||
set to :class:`~django.db.models.BigAutoField`. In a future Django release the
|
||||
default value of :setting:`DEFAULT_AUTO_FIELD` will be changed to
|
||||
:class:`~django.db.models.BigAutoField`.
|
||||
|
||||
To avoid unwanted migrations in the future, either explicitly set
|
||||
:setting:`DEFAULT_AUTO_FIELD` to :class:`~django.db.models.AutoField`::
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
|
||||
|
||||
or configure it on a per-app basis::
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
class MyAppConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.AutoField'
|
||||
name = 'my_app'
|
||||
|
||||
or on a per-model basis::
|
||||
|
||||
from django.db import models
|
||||
|
||||
class MyModel(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
|
||||
In anticipation of the changing default, a system check will provide a warning
|
||||
if you do not have an explicit setting for :setting:`DEFAULT_AUTO_FIELD`.
|
||||
|
||||
``pymemcache`` support
|
||||
----------------------
|
||||
|
||||
|
|
|
@ -259,11 +259,12 @@ details can be found in the :ref:`common model field option reference
|
|||
Automatic primary key fields
|
||||
----------------------------
|
||||
|
||||
By default, Django gives each model the following field::
|
||||
By default, Django gives each model an auto-incrementing primary key with the
|
||||
type specified per app in :attr:`AppConfig.default_auto_field
|
||||
<django.apps.AppConfig.default_auto_field>` or globally in the
|
||||
:setting:`DEFAULT_AUTO_FIELD` setting. For example::
|
||||
|
||||
id = models.AutoField(primary_key=True)
|
||||
|
||||
This is an auto-incrementing primary key.
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
|
||||
If you'd like to specify a custom primary key, specify
|
||||
:attr:`primary_key=True <Field.primary_key>` on one of your fields. If Django
|
||||
|
@ -273,6 +274,11 @@ sees you've explicitly set :attr:`Field.primary_key`, it won't add the automatic
|
|||
Each model requires exactly one field to have :attr:`primary_key=True
|
||||
<Field.primary_key>` (either explicitly declared or automatically added).
|
||||
|
||||
.. versionchanged:: 3.2
|
||||
|
||||
In older versions, auto-created primary key fields were always
|
||||
:class:`AutoField`\s.
|
||||
|
||||
.. _verbose-field-names:
|
||||
|
||||
Verbose field names
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue