diff --git a/polymorphic/base.py b/polymorphic/base.py index c4fc6918..d6586c17 100644 --- a/polymorphic/base.py +++ b/polymorphic/base.py @@ -6,7 +6,6 @@ import sys import warnings -import django from django.db import models from django.db.models.base import ModelBase @@ -53,21 +52,10 @@ class PolymorphicModelBase(ModelBase): """ def __new__(self, model_name, bases, attrs, **kwargs): - # print; print '###', model_name, '- bases:', bases - # Workaround compatibility issue with six.with_metaclass() and custom Django model metaclasses: 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) @@ -130,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 = ( f'PolymorphicModel: "{model_name}.{manager_name}" manager is of type "{type(manager).__name__}", ' f"but must be a subclass of PolymorphicManager.{extra} to support retrieving subclasses" diff --git a/polymorphic/compat.py b/polymorphic/compat.py deleted file mode 100644 index ee7f1395..00000000 --- a/polymorphic/compat.py +++ /dev/null @@ -1,9 +0,0 @@ -"""Compatibility with Python 2 (taken from 'django.utils.six')""" - - -def with_metaclass(meta, *bases): - class metaclass(type): - def __new__(cls, name, this_bases, d): - return meta(name, bases, d) - - return type.__new__(metaclass, "temporary_class", (), {}) diff --git a/polymorphic/formsets/utils.py b/polymorphic/formsets/utils.py index f78aee7e..f9c31ea6 100644 --- a/polymorphic/formsets/utils.py +++ b/polymorphic/formsets/utils.py @@ -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) diff --git a/polymorphic/models.py b/polymorphic/models.py index 0c31b339..0569e417 100644 --- a/polymorphic/models.py +++ b/polymorphic/models.py @@ -6,8 +6,6 @@ from django.db.models.fields.related import ForwardManyToOneDescriptor, ReverseOneToOneDescriptor from django.db.utils import DEFAULT_DB_ALIAS -from polymorphic.compat import with_metaclass - from .base import PolymorphicModelBase from .managers import PolymorphicManager from .query_translate import translate_polymorphic_Q_object @@ -24,7 +22,7 @@ class PolymorphicTypeInvalid(RuntimeError): pass -class PolymorphicModel(with_metaclass(PolymorphicModelBase, models.Model)): +class PolymorphicModel(models.Model, metaclass=PolymorphicModelBase): """ Abstract base class that provides polymorphic behaviour for any model directly or indirectly derived from it. diff --git a/polymorphic/query.py b/polymorphic/query.py index 9736256e..5e811611 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -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 @@ -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.""" diff --git a/polymorphic/query_translate.py b/polymorphic/query_translate.py index 89c00953..2abe9174 100644 --- a/polymorphic/query_translate.py +++ b/polymorphic/query_translate.py @@ -38,7 +38,7 @@ def translate_polymorphic_filter_definitions_in_kwargs( Returns: a list of non-keyword-arguments (Q objects) to be added to the filter() query. """ additional_args = [] - for field_path, val in kwargs.copy().items(): # Python 3 needs copy + for field_path, val in kwargs.copy().items(): # `copy` so we're not mutating the dict new_expr = _translate_polymorphic_filter_definition( queryset_model, field_path, val, using=using ) diff --git a/polymorphic/tests/models.py b/polymorphic/tests/models.py index f6462ffd..76e1b626 100644 --- a/polymorphic/tests/models.py +++ b/polymorphic/tests/models.py @@ -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): diff --git a/polymorphic/tests/test_orm.py b/polymorphic/tests/test_orm.py index 5bb837d4..98cf7d33 100644 --- a/polymorphic/tests/test_orm.py +++ b/polymorphic/tests/test_orm.py @@ -8,7 +8,7 @@ from django.db.utils import IntegrityError from django.test import TransactionTestCase -from polymorphic import compat, query_translate +from polymorphic import query_translate from polymorphic.managers import PolymorphicManager from polymorphic.models import PolymorphicTypeInvalid, PolymorphicTypeUndefined from polymorphic.tests.models import ( diff --git a/polymorphic/tests/test_regression.py b/polymorphic/tests/test_regression.py index 0f48971e..f2d74a18 100644 --- a/polymorphic/tests/test_regression.py +++ b/polymorphic/tests/test_regression.py @@ -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): @@ -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, )