Skip to content

Commit

Permalink
Remove unnecessary Django version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed Dec 20, 2023
1 parent 4240931 commit bd18ac5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 65 deletions.
15 changes: 1 addition & 14 deletions polymorphic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys
import warnings

import django
from django.db import models
from django.db.models.base import ModelBase

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

# Make sure that manager_inheritance_from_future is set, since django-polymorphic 1.x already
# simulated that behavior on the polymorphic manager to all subclasses behave like polymorphics
if django.VERSION < (2, 0):
if "Meta" in attrs:
if not hasattr(attrs["Meta"], "manager_inheritance_from_future"):
attrs["Meta"].manager_inheritance_from_future = True
else:
attrs["Meta"] = type("Meta", (object,), {"manager_inheritance_from_future": True})

# create new model
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)

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

if not issubclass(type(manager), PolymorphicManager):
if django.VERSION < (2, 0):
extra = "\nConsider using Meta.manager_inheritance_from_future = True for Django 1.x projects"
else:
extra = ""
extra = ""
e = (
'PolymorphicModel: "{0}.{1}" manager is of type "{2}", but must be a subclass of'
" PolymorphicManager.{extra} to support retrieving subclasses".format(
Expand Down
13 changes: 2 additions & 11 deletions polymorphic/formsets/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
"""
Internal utils
"""
import django


def add_media(dest, media):
"""
Optimized version of django.forms.Media.__add__() that doesn't create new objects.
"""
if django.VERSION >= (2, 2):
dest._css_lists.extend(media._css_lists)
dest._js_lists.extend(media._js_lists)
elif django.VERSION >= (2, 0):
combined = dest + media
dest._css = combined._css
dest._js = combined._js
else:
dest.add_css(media._css)
dest.add_js(media._js)
dest._css_lists.extend(media._css_lists)
dest._js_lists.extend(media._js_lists)
41 changes: 11 additions & 30 deletions polymorphic/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import copy
from collections import defaultdict

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

# Makes _filter_or_exclude compatible with the change in signature introduced in django at 9c9a3fe
if get_django_version() >= "3.2":

def _filter_or_exclude(self, negate, args, kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
queryset_model=self.model, args=args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
queryset_model=self.model, kwargs=kwargs, using=self.db
)
args = list(q_objects) + additional_args
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)

else:

def _filter_or_exclude(self, negate, *args, **kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
self.model, args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
self.model, kwargs, using=self.db
)
return super()._filter_or_exclude(
negate, *(list(q_objects) + additional_args), **kwargs
)
def _filter_or_exclude(self, negate, args, kwargs):
# We override this internal Django function as it is used for all filter member functions.
q_objects = translate_polymorphic_filter_definitions_in_args(
queryset_model=self.model, args=args, using=self.db
)
# filter_field='data'
additional_args = translate_polymorphic_filter_definitions_in_kwargs(
queryset_model=self.model, kwargs=kwargs, using=self.db
)
args = list(q_objects) + additional_args
return super()._filter_or_exclude(negate=negate, args=args, kwargs=kwargs)

def order_by(self, *field_names):
"""translate the field paths in the args, then call vanilla order_by."""
Expand Down
5 changes: 1 addition & 4 deletions polymorphic/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ class MROBase3(models.Model):


class MRODerived(MROBase2, MROBase3):
if django.VERSION < (3, 0):

class Meta:
manager_inheritance_from_future = True
pass


class ParentModelWithManager(PolymorphicModel):
Expand Down
15 changes: 9 additions & 6 deletions polymorphic/tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import django
from django.test import TestCase

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

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


class RegressionTests(TestCase):
def test_for_query_result_incomplete_with_inheritance(self):
Expand All @@ -19,15 +16,21 @@ def test_for_query_result_incomplete_with_inheritance(self):

expected_queryset = [top, middle, bottom]
self.assertQuerySetEqual(
Top.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Top.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

expected_queryset = [middle, bottom]
self.assertQuerySetEqual(
Middle.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Middle.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

expected_queryset = [bottom]
self.assertQuerySetEqual(
Bottom.objects.order_by("pk"), [repr(r) for r in expected_queryset], **transform_arg
Bottom.objects.order_by("pk"),
[repr(r) for r in expected_queryset],
transform=repr,
)

0 comments on commit bd18ac5

Please sign in to comment.