1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #31223 -- Added __class_getitem__() to Manager and QuerySet.

This commit is contained in:
sobolevn 2020-02-02 13:15:58 +03:00 committed by Carlton Gibson
parent fc0b48d2e7
commit 578c03b276
4 changed files with 21 additions and 0 deletions

View File

@ -35,6 +35,9 @@ class BaseManager:
"""Return "app_label.model_label.manager_name".""" """Return "app_label.model_label.manager_name"."""
return '%s.%s' % (self.model._meta.label, self.name) return '%s.%s' % (self.model._meta.label, self.name)
def __class_getitem__(cls, *args, **kwargs):
return cls
def deconstruct(self): def deconstruct(self):
""" """
Return a 5-tuple of the form (as_manager (True), manager_class, Return a 5-tuple of the form (as_manager (True), manager_class,

View File

@ -323,6 +323,9 @@ class QuerySet:
qs._fetch_all() qs._fetch_all()
return qs._result_cache[0] return qs._result_cache[0]
def __class_getitem__(cls, *args, **kwargs):
return cls
def __and__(self, other): def __and__(self, other):
self._merge_sanity_check(other) self._merge_sanity_check(other)
if isinstance(other, EmptyQuerySet): if isinstance(other, EmptyQuerySet):

View File

@ -1,7 +1,10 @@
from unittest import skipUnless
from django.db import models from django.db import models
from django.template import Context, Template from django.template import Context, Template
from django.test import SimpleTestCase, TestCase, override_settings from django.test import SimpleTestCase, TestCase, override_settings
from django.test.utils import isolate_apps from django.test.utils import isolate_apps
from django.utils.version import PY37
from .models import ( from .models import (
AbstractBase1, AbstractBase2, AbstractBase3, Child1, Child2, Child3, AbstractBase1, AbstractBase2, AbstractBase3, Child1, Child2, Child3,
@ -285,3 +288,7 @@ class TestManagerInheritance(SimpleTestCase):
self.assertEqual(TestModel._meta.managers, (TestModel.custom_manager,)) self.assertEqual(TestModel._meta.managers, (TestModel.custom_manager,))
self.assertEqual(TestModel._meta.managers_map, {'custom_manager': TestModel.custom_manager}) self.assertEqual(TestModel._meta.managers_map, {'custom_manager': TestModel.custom_manager})
@skipUnless(PY37, '__class_getitem__() was added in Python 3.7')
def test_manager_class_getitem(self):
self.assertIs(models.Manager[Child1], models.Manager)

View File

@ -1,9 +1,11 @@
from operator import attrgetter from operator import attrgetter
from unittest import skipUnless
from django.core.exceptions import FieldError, ValidationError from django.core.exceptions import FieldError, ValidationError
from django.db import connection, models from django.db import connection, models
from django.test import SimpleTestCase, TestCase from django.test import SimpleTestCase, TestCase
from django.test.utils import CaptureQueriesContext, isolate_apps from django.test.utils import CaptureQueriesContext, isolate_apps
from django.utils.version import PY37
from .models import ( from .models import (
Base, Chef, CommonInfo, GrandChild, GrandParent, ItalianRestaurant, Base, Chef, CommonInfo, GrandChild, GrandParent, ItalianRestaurant,
@ -217,6 +219,12 @@ class ModelInheritanceTests(TestCase):
self.assertSequenceEqual(qs, [p2, p1]) self.assertSequenceEqual(qs, [p2, p1])
self.assertIn(expected_order_by_sql, str(qs.query)) self.assertIn(expected_order_by_sql, str(qs.query))
@skipUnless(PY37, '__class_getitem__() was added in Python 3.7')
def test_queryset_class_getitem(self):
self.assertIs(models.QuerySet[Post], models.QuerySet)
self.assertIs(models.QuerySet[Post, Post], models.QuerySet)
self.assertIs(models.QuerySet[Post, int, str], models.QuerySet)
class ModelInheritanceDataTests(TestCase): class ModelInheritanceDataTests(TestCase):
@classmethod @classmethod