Fix compatibility with older Django versions

fix_request_path_info
Diederik van der Boor 2016-08-10 13:51:30 +02:00
parent 3817061467
commit 10ab3ad14c
2 changed files with 17 additions and 9 deletions

View File

@ -1,8 +1,15 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import django import django
import re
from django.db import models from django.db import models
from django.utils import six 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 @python_2_unicode_compatible
@ -95,7 +102,7 @@ class ShowFieldBase(object):
# ( bool: new section , item-text , separator to use after item ) # ( bool: new section , item-text , separator to use after item )
# start with model name # start with model name
parts = [(True, self._meta.object_name, ':')] parts = [(True, RE_DEFERRED.sub('', self.__class__.__name__), ':')]
# add all regular fields # add all regular fields
self._showfields_add_regular_fields(parts) self._showfields_add_regular_fields(parts)
@ -152,8 +159,8 @@ class ShowFieldBase(object):
if django.VERSION < (1, 8): if django.VERSION < (1, 8):
def get_deferred_fields(self): def get_deferred_fields(self):
from django.db.models import DeferredAttribute from django.db.models.query_utils import DeferredAttribute
return set(attr for attr, value in self.__class__ if isinstance(value, DeferredAttribute)) return set(attr for attr, value in self.__class__.__dict__.items() if isinstance(value, DeferredAttribute))
class ShowFieldType(ShowFieldBase): class ShowFieldType(ShowFieldBase):

View File

@ -1139,10 +1139,11 @@ class PolymorphicTests(TestCase):
self.assertEqual(result, {'cnt': 2}) self.assertEqual(result, {'cnt': 2})
# aggregate using **args # aggregate using **args
with self.assertRaisesMessage(AssertionError, 'PolymorphicModel: annotate()/aggregate(): ___ model lookup supported for keyword arguments only'): self.assertRaisesMessage(
Model2A.objects.aggregate(Count('Model2B___field2')) 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") @skipIf(django.VERSION < (1,8,), "This test needs Django >=1.8")
def test_polymorphic__complex_aggregate(self): def test_polymorphic__complex_aggregate(self):
@ -1294,7 +1295,7 @@ def qrepr(data):
""" """
Ensure consistent repr() output for the QuerySet object. Ensure consistent repr() output for the QuerySet object.
""" """
if isinstance(data, models.QuerySet): if isinstance(data, QuerySet):
if django.VERSION >= (1, 10): if django.VERSION >= (1, 10):
return repr(data) return repr(data)
else: else: