From 0de89b6f8d4d8f5fd232d6c5412260b0d79a760a Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Fri, 22 Apr 2022 08:13:12 -0400 Subject: [PATCH] Refs #31223 -- Added __class_getitem__() to ForeignKey. --- django/db/models/fields/related.py | 3 +++ tests/model_fields/test_foreignkey.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index ca4f4368b6..fad5560486 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -978,6 +978,9 @@ class ForeignKey(ForeignObject): ) self.db_constraint = db_constraint + def __class_getitem__(cls, *args, **kwargs): + return cls + def check(self, **kwargs): return [ *super().check(**kwargs), diff --git a/tests/model_fields/test_foreignkey.py b/tests/model_fields/test_foreignkey.py index a5b646ae86..ca8eff3540 100644 --- a/tests/model_fields/test_foreignkey.py +++ b/tests/model_fields/test_foreignkey.py @@ -164,3 +164,6 @@ class ForeignKeyTests(TestCase): class MyModel(models.Model): child = models.ForeignKey(1, models.CASCADE) + + def test_manager_class_getitem(self): + self.assertIs(models.ForeignKey["Foo"], models.ForeignKey)