From aead57c037089b3465bf406d0c7c66735df3ad7b Mon Sep 17 00:00:00 2001 From: Diederik van der Boor Date: Thu, 16 Oct 2014 16:04:53 +0200 Subject: [PATCH] Fix Django 1.4/1.5 issues with RelatedManager code that overrides get_query_set() Make sure the `RelatedManager.get_query_set()` is called, which limits the set, instead of calling `self.get_queryset()` which then returns a new queryset. Fortunately, this only happened on proxied calls for unknown methods. --- polymorphic/manager.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/polymorphic/manager.py b/polymorphic/manager.py index 56ba26a..138a707 100644 --- a/polymorphic/manager.py +++ b/polymorphic/manager.py @@ -38,6 +38,15 @@ class PolymorphicManager(models.Manager): if django.VERSION < (1, 7): get_query_set = get_queryset + if django.VERSION >= (1,6): + # Should not be used for Django 1.4/1.5 all all, as that breaks the RelatedManager + def _get_queryset(self): + return self.get_queryset() + else: + # Django 1.5 + def _get_queryset(self): + return self.get_query_set() + # 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. @@ -45,7 +54,7 @@ class PolymorphicManager(models.Manager): def __getattr__(self, name): if name.startswith('__'): return super(PolymorphicManager, self).__getattr__(self, name) - return getattr(self.get_queryset(), name) + return getattr(self._get_queryset(), name) def __unicode__(self): return '%s (PolymorphicManager) using %s' % (self.__class__.__name__, self.queryset_class.__name__)