From b5e12d490af3debca8c55ab3c1698189fdedbbdb Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Sun, 12 Jul 2020 13:59:57 +0100 Subject: 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. --- django/db/models/base.py | 25 +++++++++++++++++++++++++ django/db/models/options.py | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) (limited to 'django/db/models') diff --git a/django/db/models/base.py b/django/db/models/base.py index de044886c3..822aad080d 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -1290,10 +1290,35 @@ class Model(metaclass=ModelBase): *cls._check_indexes(databases), *cls._check_ordering(), *cls._check_constraints(databases), + *cls._check_default_pk(), ] return errors + @classmethod + def _check_default_pk(cls): + if ( + cls._meta.pk.auto_created and + not settings.is_overridden('DEFAULT_AUTO_FIELD') 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_swappable(cls): """Check if the swapped model exists.""" diff --git a/django/db/models/options.py b/django/db/models/options.py index 0e28b6812a..4028e05b99 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -5,12 +5,13 @@ from collections import defaultdict from django.apps import apps from django.conf import settings -from django.core.exceptions import FieldDoesNotExist +from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured from django.db import connections from django.db.models import AutoField, Manager, OrderWrt, UniqueConstraint from django.db.models.query_utils import PathInfo from django.utils.datastructures import ImmutableList, OrderedSet from django.utils.functional import cached_property +from django.utils.module_loading import import_string from django.utils.text import camel_case_to_spaces, format_lazy from django.utils.translation import override @@ -217,6 +218,37 @@ class Options: new_objs.append(obj) return new_objs + def _get_default_pk_class(self): + pk_class_path = getattr( + self.app_config, + 'default_auto_field', + settings.DEFAULT_AUTO_FIELD, + ) + if self.app_config and self.app_config._is_default_auto_field_overridden: + app_config_class = type(self.app_config) + source = ( + f'{app_config_class.__module__}.' + f'{app_config_class.__qualname__}.default_auto_field' + ) + else: + source = 'DEFAULT_AUTO_FIELD' + if not pk_class_path: + raise ImproperlyConfigured(f'{source} must not be empty.') + try: + pk_class = import_string(pk_class_path) + except ImportError as e: + msg = ( + f"{source} refers to the module '{pk_class_path}' that could " + f"not be imported." + ) + raise ImproperlyConfigured(msg) from e + if not issubclass(pk_class, AutoField): + raise ValueError( + f"Primary key '{pk_class_path}' referred by {source} must " + f"subclass AutoField." + ) + return pk_class + def _prepare(self, model): if self.order_with_respect_to: # The app registry will not be ready at this point, so we cannot @@ -250,7 +282,8 @@ class Options: field.primary_key = True self.setup_pk(field) else: - auto = AutoField(verbose_name='ID', primary_key=True, auto_created=True) + pk_class = self._get_default_pk_class() + auto = pk_class(verbose_name='ID', primary_key=True, auto_created=True) model.add_to_class('id', auto) def add_manager(self, manager): -- cgit v1.3