diff --git a/polymorphic/base.py b/polymorphic/base.py index d9b2ff7..8c6b154 100644 --- a/polymorphic/base.py +++ b/polymorphic/base.py @@ -12,7 +12,7 @@ from django.db import models from django.db.models.base import ModelBase from django.db.models.manager import ManagerDescriptor -from .manager import PolymorphicManager +from .managers import PolymorphicManager from .query import PolymorphicQuerySet # PolymorphicQuerySet Q objects (and filter()) support these additional key words. diff --git a/polymorphic/manager.py b/polymorphic/manager.py index 06f52c8..3e3e35d 100644 --- a/polymorphic/manager.py +++ b/polymorphic/manager.py @@ -1,55 +1,2 @@ -# -*- coding: utf-8 -*- -""" PolymorphicManager - Please see README.rst or DOCS.rst or http://chrisglass.github.com/django_polymorphic/ -""" -from __future__ import unicode_literals -import warnings -import django -from django.db import models -from polymorphic.query import PolymorphicQuerySet - - -class PolymorphicManager(models.Manager): - """ - Manager for PolymorphicModel - - Usually not explicitly needed, except if a custom manager or - a custom queryset class is to be used. - """ - # Tell Django that related fields also need to use this manager: - use_for_related_fields = True - queryset_class = PolymorphicQuerySet - - def __init__(self, queryset_class=None, *args, **kwrags): - # Up till polymorphic 0.4, the queryset class could be specified as parameter to __init__. - # However, this doesn't work for related managers which instantiate a new version of this class. - # Hence, for custom managers the new default is using the 'queryset_class' attribute at class level instead. - if queryset_class: - warnings.warn("Using PolymorphicManager(queryset_class=..) is deprecated; override the queryset_class attribute instead", DeprecationWarning) - # For backwards compatibility, still allow the parameter: - self.queryset_class = queryset_class - - super(PolymorphicManager, self).__init__(*args, **kwrags) - - def get_queryset(self): - return self.queryset_class(self.model, using=self._db) - - # For Django 1.5 - if django.VERSION < (1, 7): - get_query_set = get_queryset - - # Proxy all unknown method calls to the queryset, so that its members are - # directly accessible as PolymorphicModel.objects.* - # The advantage of this method is that not yet known member functions of derived querysets will be proxied as well. - # We exclude any special functions (__) from this automatic proxying. - # - # NOTE: Fetching the queryset is done by calling self.all() here on purpose. - # By using .all(), the proper get_query_set()/get_queryset() will be used for each Django version. - # Django 1.4/1.5 need to use get_query_set(), because the RelatedManager overrides that. - def __getattr__(self, name): - if name.startswith('__'): - return super(PolymorphicManager, self).__getattr__(self, name) - return getattr(self.all(), name) - - def __unicode__(self): - return '%s (PolymorphicManager) using %s' % (self.__class__.__name__, self.queryset_class.__name__) +# For compatibility with pre 0.8 versions +from .managers import PolymorphicQuerySet, PolymorphicManager diff --git a/polymorphic/managers.py b/polymorphic/managers.py new file mode 100644 index 0000000..06f52c8 --- /dev/null +++ b/polymorphic/managers.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +""" PolymorphicManager + Please see README.rst or DOCS.rst or http://chrisglass.github.com/django_polymorphic/ +""" +from __future__ import unicode_literals +import warnings +import django +from django.db import models +from polymorphic.query import PolymorphicQuerySet + + +class PolymorphicManager(models.Manager): + """ + Manager for PolymorphicModel + + Usually not explicitly needed, except if a custom manager or + a custom queryset class is to be used. + """ + # Tell Django that related fields also need to use this manager: + use_for_related_fields = True + queryset_class = PolymorphicQuerySet + + def __init__(self, queryset_class=None, *args, **kwrags): + # Up till polymorphic 0.4, the queryset class could be specified as parameter to __init__. + # However, this doesn't work for related managers which instantiate a new version of this class. + # Hence, for custom managers the new default is using the 'queryset_class' attribute at class level instead. + if queryset_class: + warnings.warn("Using PolymorphicManager(queryset_class=..) is deprecated; override the queryset_class attribute instead", DeprecationWarning) + # For backwards compatibility, still allow the parameter: + self.queryset_class = queryset_class + + super(PolymorphicManager, self).__init__(*args, **kwrags) + + def get_queryset(self): + return self.queryset_class(self.model, using=self._db) + + # For Django 1.5 + if django.VERSION < (1, 7): + get_query_set = get_queryset + + # Proxy all unknown method calls to the queryset, so that its members are + # directly accessible as PolymorphicModel.objects.* + # The advantage of this method is that not yet known member functions of derived querysets will be proxied as well. + # We exclude any special functions (__) from this automatic proxying. + # + # NOTE: Fetching the queryset is done by calling self.all() here on purpose. + # By using .all(), the proper get_query_set()/get_queryset() will be used for each Django version. + # Django 1.4/1.5 need to use get_query_set(), because the RelatedManager overrides that. + def __getattr__(self, name): + if name.startswith('__'): + return super(PolymorphicManager, self).__getattr__(self, name) + return getattr(self.all(), name) + + def __unicode__(self): + return '%s (PolymorphicManager) using %s' % (self.__class__.__name__, self.queryset_class.__name__) diff --git a/polymorphic/models.py b/polymorphic/models.py index 85d8d28..eebcc02 100644 --- a/polymorphic/models.py +++ b/polymorphic/models.py @@ -20,7 +20,7 @@ from django.contrib.contenttypes.models import ContentType from django.utils import six from .base import PolymorphicModelBase -from .manager import PolymorphicManager +from .managers import PolymorphicManager from .query_translate import translate_polymorphic_Q_object ################################################################################### diff --git a/polymorphic/query.py b/polymorphic/query.py index 1d229ab..068ef47 100644 --- a/polymorphic/query.py +++ b/polymorphic/query.py @@ -75,7 +75,7 @@ class PolymorphicQuerySet(QuerySet): if django.VERSION >= (1, 7): def as_manager(cls): # Make sure the Django 1.7 way of creating managers works. - from .manager import PolymorphicManager + from .managers import PolymorphicManager manager = PolymorphicManager.from_queryset(cls)() manager._built_with_as_manager = True return manager diff --git a/polymorphic/tests.py b/polymorphic/tests.py index b2da0eb..0bd1e17 100644 --- a/polymorphic/tests.py +++ b/polymorphic/tests.py @@ -20,7 +20,7 @@ from django.contrib.contenttypes.models import ContentType from django.utils import six from polymorphic.models import PolymorphicModel -from polymorphic.manager import PolymorphicManager +from polymorphic.managers import PolymorphicManager from polymorphic.query import PolymorphicQuerySet from polymorphic import ShowFieldContent, ShowFieldType, ShowFieldTypeAndContent try: @@ -588,11 +588,11 @@ class PolymorphicTests(TestCase): self.assertEqual(show_base_manager(PlainB), " ") self.assertEqual(show_base_manager(PlainC), " ") - self.assertEqual(show_base_manager(Model2A), " ") + self.assertEqual(show_base_manager(Model2A), " ") self.assertEqual(show_base_manager(Model2B), " ") self.assertEqual(show_base_manager(Model2C), " ") - self.assertEqual(show_base_manager(One2OneRelatingModel), " ") + self.assertEqual(show_base_manager(One2OneRelatingModel), " ") self.assertEqual(show_base_manager(One2OneRelatingModelDerived), " ") def test_instance_default_manager(self): @@ -614,9 +614,9 @@ class PolymorphicTests(TestCase): self.assertEqual(show_default_manager(plain_b), " ") self.assertEqual(show_default_manager(plain_c), " ") - self.assertEqual(show_default_manager(model_2a), " ") - self.assertEqual(show_default_manager(model_2b), " ") - self.assertEqual(show_default_manager(model_2c), " ") + self.assertEqual(show_default_manager(model_2a), " ") + self.assertEqual(show_default_manager(model_2b), " ") + self.assertEqual(show_default_manager(model_2c), " ") def test_foreignkey_field(self): self.create_model2abcd()