From 45a58c31e64dbfdecab1178b1d00a3803a90ea2d Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 8 Apr 2021 11:40:16 +0100 Subject: [PATCH] Fixed #32620 -- Allowed subclasses of Big/SmallAutoField for DEFAULT_AUTO_FIELD. --- django/db/models/fields/__init__.py | 2 +- docs/releases/3.2.1.txt | 4 ++++ tests/model_fields/test_autofield.py | 14 +++++++++++++- tests/model_options/test_default_pk.py | 13 +++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 0f53d9c30bf..0ce7de02c63 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -2524,7 +2524,7 @@ class AutoFieldMeta(type): return isinstance(instance, self._subclasses) or super().__instancecheck__(instance) def __subclasscheck__(self, subclass): - return subclass in self._subclasses or super().__subclasscheck__(subclass) + return issubclass(subclass, self._subclasses) or super().__subclasscheck__(subclass) class AutoField(AutoFieldMixin, IntegerField, metaclass=AutoFieldMeta): diff --git a/docs/releases/3.2.1.txt b/docs/releases/3.2.1.txt index d303b46d4da..e8361e73702 100644 --- a/docs/releases/3.2.1.txt +++ b/docs/releases/3.2.1.txt @@ -10,3 +10,7 @@ Bugfixes ======== * Corrected detection of GDAL 3.2 on Windows (:ticket:`32544`). + +* Fixed a bug in Django 3.2 where subclasses of ``BigAutoField`` and + ``SmallAutoField`` were not allowed for the :setting:`DEFAULT_AUTO_FIELD` + setting (:ticket:`32620`). diff --git a/tests/model_fields/test_autofield.py b/tests/model_fields/test_autofield.py index 646cd2ab0a5..0ae981c3d97 100644 --- a/tests/model_fields/test_autofield.py +++ b/tests/model_fields/test_autofield.py @@ -30,6 +30,18 @@ class AutoFieldInheritanceTests(SimpleTestCase): self.assertIsInstance(field(), models.AutoField) def test_issubclass_of_autofield(self): - for field in (models.BigAutoField, models.SmallAutoField): + class MyBigAutoField(models.BigAutoField): + pass + + class MySmallAutoField(models.SmallAutoField): + pass + + tests = [ + MyBigAutoField, + MySmallAutoField, + models.BigAutoField, + models.SmallAutoField, + ] + for field in tests: with self.subTest(field.__name__): self.assertTrue(issubclass(field, models.AutoField)) diff --git a/tests/model_options/test_default_pk.py b/tests/model_options/test_default_pk.py index b6788489160..a3beaba7c3a 100644 --- a/tests/model_options/test_default_pk.py +++ b/tests/model_options/test_default_pk.py @@ -4,6 +4,10 @@ from django.test import SimpleTestCase, override_settings from django.test.utils import isolate_apps +class MyBigAutoField(models.BigAutoField): + pass + + @isolate_apps('model_options') class TestDefaultPK(SimpleTestCase): @override_settings(DEFAULT_AUTO_FIELD='django.db.models.NonexistentAutoField') @@ -74,6 +78,15 @@ class TestDefaultPK(SimpleTestCase): self.assertIsInstance(Model._meta.pk, models.SmallAutoField) + @override_settings( + DEFAULT_AUTO_FIELD='model_options.test_default_pk.MyBigAutoField' + ) + def test_default_auto_field_setting_bigautofield_subclass(self): + class Model(models.Model): + pass + + self.assertIsInstance(Model._meta.pk, MyBigAutoField) + @isolate_apps('model_options.apps.ModelPKConfig') @override_settings(DEFAULT_AUTO_FIELD='django.db.models.AutoField') def test_app_default_auto_field(self):