Skip to content

Commit bd18ac5

Browse files
committed
Remove unnecessary Django version checks
1 parent 4240931 commit bd18ac5

File tree

5 files changed

+24
-65
lines changed

5 files changed

+24
-65
lines changed

polymorphic/base.py

Lines changed: 1 addition & 14 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

@@ -59,15 +58,6 @@ def __new__(self, model_name, bases, attrs, **kwargs):
5958
if not attrs and model_name == "NewBase":
6059
return super().__new__(self, model_name, bases, attrs, **kwargs)
6160

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-
7161
# create new model
7262
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)
7363

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

130120
if not issubclass(type(manager), PolymorphicManager):
131-
if django.VERSION < (2, 0):
132-
extra = "\nConsider using Meta.manager_inheritance_from_future = True for Django 1.x projects"
133-
else:
134-
extra = ""
121+
extra = ""
135122
e = (
136123
'PolymorphicModel: "{0}.{1}" manager is of type "{2}", but must be a subclass of'
137124
" PolymorphicManager.{extra} to support retrieving subclasses".format(

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/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/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_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)