From 10ab3ad14c71f04dcc50bd65ee09838a6a7e7819 Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Wed, 10 Aug 2016 13:51:30 +0200 Subject: [PATCH] Fix compatibility with older Django versions --- polymorphic/showfields.py | 15 +++++++++++---- polymorphic/tests.py | 11 ++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/polymorphic/showfields.py b/polymorphic/showfields.py index fdb4bcf..a7270d5 100644 --- a/polymorphic/showfields.py +++ b/polymorphic/showfields.py @@ -1,8 +1,15 @@ # -*- coding: utf-8 -*- import django +import re from django.db import models from django.utils import six -from django.utils.six import python_2_unicode_compatible + +try: + from django.utils.six import python_2_unicode_compatible +except ImportError: + from django.utils.encoding import python_2_unicode_compatible # Django 1.5 + +RE_DEFERRED = re.compile('_Deferred_.*') @python_2_unicode_compatible @@ -95,7 +102,7 @@ class ShowFieldBase(object): # ( bool: new section , item-text , separator to use after item ) # start with model name - parts = [(True, self._meta.object_name, ':')] + parts = [(True, RE_DEFERRED.sub('', self.__class__.__name__), ':')] # add all regular fields self._showfields_add_regular_fields(parts) @@ -152,8 +159,8 @@ class ShowFieldBase(object): if django.VERSION < (1, 8): def get_deferred_fields(self): - from django.db.models import DeferredAttribute - return set(attr for attr, value in self.__class__ if isinstance(value, DeferredAttribute)) + from django.db.models.query_utils import DeferredAttribute + return set(attr for attr, value in self.__class__.__dict__.items() if isinstance(value, DeferredAttribute)) class ShowFieldType(ShowFieldBase): diff --git a/polymorphic/tests.py b/polymorphic/tests.py index 38ad70c..cfddd90 100644 --- a/polymorphic/tests.py +++ b/polymorphic/tests.py @@ -1139,10 +1139,11 @@ class PolymorphicTests(TestCase): self.assertEqual(result, {'cnt': 2}) # aggregate using **args - with self.assertRaisesMessage(AssertionError, 'PolymorphicModel: annotate()/aggregate(): ___ model lookup supported for keyword arguments only'): - Model2A.objects.aggregate(Count('Model2B___field2')) - - + self.assertRaisesMessage( + AssertionError, + 'PolymorphicModel: annotate()/aggregate(): ___ model lookup supported for keyword arguments only', + lambda: Model2A.objects.aggregate(Count('Model2B___field2')) + ) @skipIf(django.VERSION < (1,8,), "This test needs Django >=1.8") def test_polymorphic__complex_aggregate(self): @@ -1294,7 +1295,7 @@ def qrepr(data): """ Ensure consistent repr() output for the QuerySet object. """ - if isinstance(data, models.QuerySet): + if isinstance(data, QuerySet): if django.VERSION >= (1, 10): return repr(data) else: