Fixed #32620 -- Allowed subclasses of Big/SmallAutoField for DEFAULT_AUTO_FIELD.

This commit is contained in:
Adam Johnson 2021-04-08 11:40:16 +01:00 committed by Mariusz Felisiak
parent 30e123ed35
commit 45a58c31e6
4 changed files with 31 additions and 2 deletions

View File

@ -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):

View File

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

View File

@ -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))

View File

@ -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):