Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion django/conf/app_template/apps.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ from django.apps import AppConfig


class {{ camel_case_app_name }}Config(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = '{{ app_name }}'
2 changes: 1 addition & 1 deletion django/conf/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def gettext_noop(s):
DEFAULT_INDEX_TABLESPACE = ""

# Default primary key field type.
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Default X-Frame-Options header value
X_FRAME_OPTIONS = "DENY"
Expand Down
5 changes: 0 additions & 5 deletions django/conf/project_template/project_name/settings.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,3 @@ USE_TZ = True
# https://docs.djangoproject.com/en/{{ docs_version }}/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
9 changes: 9 additions & 0 deletions django/db/backends/postgresql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ def django_test_expected_failures(self):
"test_group_by_nested_expression_with_params",
}
)
if not is_psycopg3:
expected_failures.update(
{
# operator does not exist: bigint[] = integer[]
"postgres_tests.test_array.TestQuerying.test_gt",
"postgres_tests.test_array.TestQuerying.test_in",
"postgres_tests.test_array.TestQuerying.test_lt",
}
)
return expected_failures

@cached_property
Expand Down
33 changes: 0 additions & 33 deletions django/db/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1701,45 +1701,12 @@ def check(cls, **kwargs):
*cls._check_indexes(databases),
*cls._check_ordering(),
*cls._check_constraints(databases),
*cls._check_default_pk(),
*cls._check_db_table_comment(databases),
*cls._check_composite_pk(),
]

return errors

@classmethod
def _check_default_pk(cls):
if (
not cls._meta.abstract
and cls._meta.pk.auto_created
and
# Inherited PKs are checked in parents models.
not (
isinstance(cls._meta.pk, OneToOneField)
and cls._meta.pk.remote_field.parent_link
)
and not settings.is_overridden("DEFAULT_AUTO_FIELD")
and cls._meta.app_config
and not cls._meta.app_config._is_default_auto_field_overridden
):
return [
checks.Warning(
f"Auto-created primary key used when not defining a "
f"primary key type, by default "
f"'{settings.DEFAULT_AUTO_FIELD}'.",
hint=(
f"Configure the DEFAULT_AUTO_FIELD setting or the "
f"{cls._meta.app_config.__class__.__qualname__}."
f"default_auto_field attribute to point to a subclass "
f"of AutoField, e.g. 'django.db.models.BigAutoField'."
),
obj=cls,
id="models.W042",
),
]
return []

@classmethod
def _check_composite_pk(cls):
errors = []
Expand Down
1 change: 0 additions & 1 deletion docs/intro/reusable-apps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ this. For a small app like polls, this process isn't too difficult.


class PollsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "django_polls"
label = "polls"

Expand Down
3 changes: 2 additions & 1 deletion docs/ref/checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ Models
* **models.E042**: ``<field name>`` cannot be included in the composite
primary key.
* **models.W042**: Auto-created primary key used when not defining a primary
key type, by default ``django.db.models.AutoField``.
key type, by default ``django.db.models.AutoField``. *This check appeared in
Django 3.2 - 5.2*.
* **models.W043**: ``<database>`` does not support indexes on expressions.
* **models.W044**: ``<database>`` does not support unique constraints on
expressions.
Expand Down
7 changes: 6 additions & 1 deletion docs/ref/settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1279,11 +1279,16 @@ See also :setting:`NUMBER_GROUPING`, :setting:`THOUSAND_SEPARATOR` and
``DEFAULT_AUTO_FIELD``
----------------------

Default: ``'``:class:`django.db.models.AutoField`\ ``'``
Default: ``'``:class:`django.db.models.BigAutoField`\ ``'``

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>`.

.. versionchanged:: 6.0

In older versions, the default value is
:class:`django.db.models.AutoField`.

.. admonition:: Migrating auto-created through tables

The value of ``DEFAULT_AUTO_FIELD`` will be respected when creating new
Expand Down
36 changes: 34 additions & 2 deletions docs/releases/6.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ Minor features
* The Font Awesome Free icon set (version 6.7.2) is now used for the admin
interface icons.

* The new :attr:`.AdminSite.password_change_form` attribute allows customizing
the form used in the admin site password change view.

:mod:`django.contrib.admindocs`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* The new :attr:`.AdminSite.password_change_form` attribute allows customizing
the form used in the admin site password change view.
* ...

:mod:`django.contrib.auth`
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -456,6 +458,36 @@ Email
significantly, closely examine any custom subclasses that rely on overriding
undocumented, internal underscore methods.

``DEFAULT_AUTO_FIELD`` setting now defaults to ``BigAutoField``
---------------------------------------------------------------

Since Django 3.2 when the :setting:`DEFAULT_AUTO_FIELD` setting was added,
the default :djadmin:`startproject` template's ``settings.py`` contained::

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

and the default :djadmin:`startapp` template's ``AppConfig`` contained::

default_auto_field = "django.db.models.BigAutoField"

At that time, the default value of :setting:`DEFAULT_AUTO_FIELD` remained
:class:`django.db.models.AutoField` for backwards compatibility.

In Django 6.0, :setting:`DEFAULT_AUTO_FIELD` now defaults to
:class:`django.db.models.BigAutoField` and the aforementioned lines in the
project and app templates are removed.

Most projects shouldn't be affected since there has been a system check warning
since Django 3.2 if a project doesn't set :setting:`DEFAULT_AUTO_FIELD`:

**models.W042**: Auto-created primary key used when not defining a primary
key type, by default ``django.db.models.AutoField``.

If you haven't dealt with this warning by now, add
``DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'`` to your project's
settings, or ``default_auto_field = 'django.db.models.AutoField'`` to an app's
``AppConfig``, as needed.

Custom ORM expressions should return params as a tuple
------------------------------------------------------

Expand Down
6 changes: 0 additions & 6 deletions tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def write_settings(self, filename, apps=None, is_dir=False, sdict=None, extra=No
settings_file.write("%s\n" % extra)
exports = [
"DATABASES",
"DEFAULT_AUTO_FIELD",
"ROOT_URLCONF",
"SECRET_KEY",
"USE_TZ",
Expand Down Expand Up @@ -3127,11 +3126,6 @@ def test_template(self):
with open(os.path.join(app_path, "apps.py")) as f:
content = f.read()
self.assertIn("class NewAppConfig(AppConfig)", content)
if HAS_BLACK:
test_str = 'default_auto_field = "django.db.models.BigAutoField"'
else:
test_str = "default_auto_field = 'django.db.models.BigAutoField'"
self.assertIn(test_str, content)
self.assertIn(
'name = "new_app"' if HAS_BLACK else "name = 'new_app'",
content,
Expand Down
10 changes: 0 additions & 10 deletions tests/check_framework/apps.py

This file was deleted.

135 changes: 0 additions & 135 deletions tests/check_framework/test_model_checks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import mock

from django.core import checks
from django.core.checks import Error, Warning
from django.db import models
Expand Down Expand Up @@ -411,136 +409,3 @@ class Meta:
constraints = [constraint]

self.assertEqual(checks.run_checks(app_configs=apps.get_app_configs()), [])


def mocked_is_overridden(self, setting):
# Force treating DEFAULT_AUTO_FIELD = 'django.db.models.AutoField' as a not
# overridden setting.
return (
setting != "DEFAULT_AUTO_FIELD"
or self.DEFAULT_AUTO_FIELD != "django.db.models.AutoField"
)


@mock.patch("django.conf.UserSettingsHolder.is_overridden", mocked_is_overridden)
@override_settings(DEFAULT_AUTO_FIELD="django.db.models.AutoField")
@isolate_apps("check_framework.apps.CheckDefaultPKConfig", attr_name="apps")
@override_system_checks([checks.model_checks.check_all_models])
class ModelDefaultAutoFieldTests(SimpleTestCase):
msg = (
"Auto-created primary key used when not defining a primary key type, "
"by default 'django.db.models.AutoField'."
)
hint = (
"Configure the DEFAULT_AUTO_FIELD setting or the "
"CheckDefaultPKConfig.default_auto_field attribute to point to a "
"subclass of AutoField, e.g. 'django.db.models.BigAutoField'."
)

def test_auto_created_pk(self):
class Model(models.Model):
pass

self.assertEqual(
checks.run_checks(app_configs=self.apps.get_app_configs()),
[
Warning(self.msg, hint=self.hint, obj=Model, id="models.W042"),
],
)

def test_explicit_inherited_pk(self):
class Parent(models.Model):
id = models.AutoField(primary_key=True)

class Child(Parent):
pass

self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), [])

def test_skipped_on_model_with_invalid_app_label(self):
class Model(models.Model):
class Meta:
app_label = "invalid_app_label"

self.assertEqual(Model.check(), [])

def test_skipped_on_abstract_model(self):
class Abstract(models.Model):
class Meta:
abstract = True

# Call .check() because abstract models are not registered.
self.assertEqual(Abstract.check(), [])

def test_explicit_inherited_parent_link(self):
class Parent(models.Model):
id = models.AutoField(primary_key=True)

class Child(Parent):
parent_ptr = models.OneToOneField(Parent, models.CASCADE, parent_link=True)

self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), [])

def test_auto_created_inherited_pk(self):
class Parent(models.Model):
pass

class Child(Parent):
pass

self.assertEqual(
checks.run_checks(app_configs=self.apps.get_app_configs()),
[
Warning(self.msg, hint=self.hint, obj=Parent, id="models.W042"),
],
)

def test_auto_created_inherited_parent_link(self):
class Parent(models.Model):
pass

class Child(Parent):
parent_ptr = models.OneToOneField(Parent, models.CASCADE, parent_link=True)

self.assertEqual(
checks.run_checks(app_configs=self.apps.get_app_configs()),
[
Warning(self.msg, hint=self.hint, obj=Parent, id="models.W042"),
],
)

def test_auto_created_pk_inherited_abstract_parent(self):
class Parent(models.Model):
class Meta:
abstract = True

class Child(Parent):
pass

self.assertEqual(
checks.run_checks(app_configs=self.apps.get_app_configs()),
[
Warning(self.msg, hint=self.hint, obj=Child, id="models.W042"),
],
)

@override_settings(DEFAULT_AUTO_FIELD="django.db.models.BigAutoField")
def test_default_auto_field_setting(self):
class Model(models.Model):
pass

self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), [])

def test_explicit_pk(self):
class Model(models.Model):
id = models.BigAutoField(primary_key=True)

self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), [])

@isolate_apps("check_framework.apps.CheckPKConfig", kwarg_name="apps")
def test_app_default_auto_field(self, apps):
class ModelWithPkViaAppConfig(models.Model):
class Meta:
app_label = "check_framework.apps.CheckPKConfig"

self.assertEqual(checks.run_checks(app_configs=apps.get_app_configs()), [])
2 changes: 1 addition & 1 deletion tests/introspection/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_get_table_description_types(self):
[
connection.features.introspected_field_types[field]
for field in (
"AutoField",
"BigAutoField",
"CharField",
"CharField",
"CharField",
Expand Down
4 changes: 2 additions & 2 deletions tests/migrations/test_migrations_no_changes/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
"Author",
[
("id", models.AutoField(primary_key=True)),
("id", models.BigAutoField(primary_key=True)),
("name", models.CharField(max_length=255)),
("slug", models.SlugField(null=True)),
("age", models.IntegerField(default=0)),
Expand All @@ -16,7 +16,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
"Tribble",
[
("id", models.AutoField(primary_key=True)),
("id", models.BigAutoField(primary_key=True)),
("fluffy", models.BooleanField(default=True)),
],
),
Expand Down
2 changes: 1 addition & 1 deletion tests/migrations/test_migrations_no_changes/0002_second.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
"Book",
[
("id", models.AutoField(primary_key=True)),
("id", models.BigAutoField(primary_key=True)),
(
"author",
models.ForeignKey("migrations.Author", models.SET_NULL, null=True),
Expand Down
Loading
Loading