Skip to content

Commit f188df0

Browse files
authored
Merge pull request #567 from akx/version-checks
Remove unnecessary version checks
2 parents 836a445 + 8ea4932 commit f188df0

File tree

9 files changed

+27
-81
lines changed

9 files changed

+27
-81
lines changed

polymorphic/base.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77
import warnings
88

9-
import django
109
from django.db import models
1110
from django.db.models.base import ModelBase
1211

@@ -53,21 +52,10 @@ class PolymorphicModelBase(ModelBase):
5352
"""
5453

5554
def __new__(self, model_name, bases, attrs, **kwargs):
56-
# print; print '###', model_name, '- bases:', bases
57-
5855
# Workaround compatibility issue with six.with_metaclass() and custom Django model metaclasses:
5956
if not attrs and model_name == "NewBase":
6057
return super().__new__(self, model_name, bases, attrs, **kwargs)
6158

62-
# Make sure that manager_inheritance_from_future is set, since django-polymorphic 1.x already
63-
# simulated that behavior on the polymorphic manager to all subclasses behave like polymorphics
64-
if django.VERSION < (2, 0):
65-
if "Meta" in attrs:
66-
if not hasattr(attrs["Meta"], "manager_inheritance_from_future"):
67-
attrs["Meta"].manager_inheritance_from_future = True
68-
else:
69-
attrs["Meta"] = type("Meta", (object,), {"manager_inheritance_from_future": True})
70-
7159
# create new model
7260
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)
7361

@@ -130,10 +118,7 @@ def validate_model_manager(self, manager, model_name, manager_name):
130118
and its querysets from PolymorphicQuerySet - throw AssertionError if not"""
131119

132120
if not issubclass(type(manager), PolymorphicManager):
133-
if django.VERSION < (2, 0):
134-
extra = "\nConsider using Meta.manager_inheritance_from_future = True for Django 1.x projects"
135-
else:
136-
extra = ""
121+
extra = ""
137122
e = (
138123
f'PolymorphicModel: "{model_name}.{manager_name}" manager is of type "{type(manager).__name__}", '
139124
f"but must be a subclass of PolymorphicManager.{extra} to support retrieving subclasses"

polymorphic/compat.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

polymorphic/formsets/utils.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
11
"""
22
Internal utils
33
"""
4-
import django
54

65

76
def add_media(dest, media):
87
"""
98
Optimized version of django.forms.Media.__add__() that doesn't create new objects.
109
"""
11-
if django.VERSION >= (2, 2):
12-
dest._css_lists.extend(media._css_lists)
13-
dest._js_lists.extend(media._js_lists)
14-
elif django.VERSION >= (2, 0):
15-
combined = dest + media
16-
dest._css = combined._css
17-
dest._js = combined._js
18-
else:
19-
dest.add_css(media._css)
20-
dest.add_js(media._js)
10+
dest._css_lists.extend(media._css_lists)
11+
dest._js_lists.extend(media._js_lists)

polymorphic/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from django.db.models.fields.related import ForwardManyToOneDescriptor, ReverseOneToOneDescriptor
77
from django.db.utils import DEFAULT_DB_ALIAS
88

9-
from polymorphic.compat import with_metaclass
10-
119
from .base import PolymorphicModelBase
1210
from .managers import PolymorphicManager
1311
from .query_translate import translate_polymorphic_Q_object
@@ -24,7 +22,7 @@ class PolymorphicTypeInvalid(RuntimeError):
2422
pass
2523

2624

27-
class PolymorphicModel(with_metaclass(PolymorphicModelBase, models.Model)):
25+
class PolymorphicModel(models.Model, metaclass=PolymorphicModelBase):
2826
"""
2927
Abstract base class that provides polymorphic behaviour
3028
for any model directly or indirectly derived from it.

polymorphic/query.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import copy
55
from collections import defaultdict
66

7-
from django import get_version as get_django_version
87
from django.contrib.contenttypes.models import ContentType
98
from django.core.exceptions import FieldDoesNotExist
109
from django.db.models import FilteredRelation
@@ -157,35 +156,17 @@ def not_instance_of(self, *args):
157156
# Implementation in _translate_polymorphic_filter_defnition."""
158157
return self.filter(not_instance_of=args)
159158

160-
# Makes _filter_or_exclude compatible with the change in signature introduced in django at 9c9a3fe
161-
if get_django_version() >= "3.2":
162-
163-
def _filter_or_exclude(self, negate, args, kwargs):
164-
# We override this internal Django function as it is used for all filter member functions.
165-
q_objects = translate_polymorphic_filter_definitions_in_args(
166-
queryset_model=self.model, args=args, using=self.db
167-
)
168-
# filter_field='data'
169-
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
170-
queryset_model=self.model, kwargs=kwargs, using=self.db
171-
)
172-
args = list(q_objects) + additional_args
173-
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)
174-
175-
else:
176-
177-
def _filter_or_exclude(self, negate, *args, **kwargs):
178-
# We override this internal Django function as it is used for all filter member functions.
179-
q_objects = translate_polymorphic_filter_definitions_in_args(
180-
self.model, args, using=self.db
181-
)
182-
# filter_field='data'
183-
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
184-
self.model, kwargs, using=self.db
185-
)
186-
return super()._filter_or_exclude(
187-
negate, *(list(q_objects) + additional_args), **kwargs
188-
)
159+
def _filter_or_exclude(self, negate, args, kwargs):
160+
# We override this internal Django function as it is used for all filter member functions.
161+
q_objects = translate_polymorphic_filter_definitions_in_args(
162+
queryset_model=self.model, args=args, using=self.db
163+
)
164+
# filter_field='data'
165+
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
166+
queryset_model=self.model, kwargs=kwargs, using=self.db
167+
)
168+
args = list(q_objects) + additional_args
169+
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)
189170

190171
def order_by(self, *field_names):
191172
"""translate the field paths in the args, then call vanilla order_by."""

polymorphic/query_translate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def translate_polymorphic_filter_definitions_in_kwargs(
3838
Returns: a list of non-keyword-arguments (Q objects) to be added to the filter() query.
3939
"""
4040
additional_args = []
41-
for field_path, val in kwargs.copy().items(): # Python 3 needs copy
41+
for field_path, val in kwargs.copy().items(): # `copy` so we're not mutating the dict
4242
new_expr = _translate_polymorphic_filter_definition(
4343
queryset_model, field_path, val, using=using
4444
)

polymorphic/tests/models.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ class MROBase3(models.Model):
206206

207207

208208
class MRODerived(MROBase2, MROBase3):
209-
if django.VERSION < (3, 0):
210-
211-
class Meta:
212-
manager_inheritance_from_future = True
209+
pass
213210

214211

215212
class ParentModelWithManager(PolymorphicModel):

polymorphic/tests/test_orm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.db.utils import IntegrityError
99
from django.test import TransactionTestCase
1010

11-
from polymorphic import compat, query_translate
11+
from polymorphic import query_translate
1212
from polymorphic.managers import PolymorphicManager
1313
from polymorphic.models import PolymorphicTypeInvalid, PolymorphicTypeUndefined
1414
from polymorphic.tests.models import (

polymorphic/tests/test_regression.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import django
21
from django.test import TestCase
32

43
from polymorphic.tests.models import Bottom, Middle, Top
54

6-
transform_arg = {"transform": repr} if django.VERSION >= (3, 2) else {}
7-
85

96
class RegressionTests(TestCase):
107
def test_for_query_result_incomplete_with_inheritance(self):
@@ -19,15 +16,21 @@ def test_for_query_result_incomplete_with_inheritance(self):
1916

2017
expected_queryset = [top, middle, bottom]
2118
self.assertQuerySetEqual(
22-
Top.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
19+
Top.objects.order_by("pk"),
20+
[repr(r) for r in expected_queryset],
21+
transform=repr,
2322
)
2423

2524
expected_queryset = [middle, bottom]
2625
self.assertQuerySetEqual(
27-
Middle.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
26+
Middle.objects.order_by("pk"),
27+
[repr(r) for r in expected_queryset],
28+
transform=repr,
2829
)
2930

3031
expected_queryset = [bottom]
3132
self.assertQuerySetEqual(
32-
Bottom.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
33+
Bottom.objects.order_by("pk"),
34+
[repr(r) for r in expected_queryset],
35+
transform=repr,
3336
)

0 commit comments

Comments
 (0)