Skip to content

Commit

Permalink
Issue #200 - Exclude super-class from proxy model query sets
Browse files Browse the repository at this point in the history
These changes prevent the query sets on 'proxy' models from including
the items from the 'super-class'.
  • Loading branch information
Andrew Dodd authored and Andrew Dodd committed May 4, 2016
1 parent 75646f1 commit 120f124
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion polymorphic/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def __init__(self, queryset_class=None, *args, **kwrags):
super(PolymorphicManager, self).__init__(*args, **kwrags)

def get_queryset(self):
return self.queryset_class(self.model, using=self._db)
qs = self.queryset_class(self.model, using=self._db)
if self.model._meta.proxy:
qs = qs.instance_of(self.model)
return qs

# For Django 1.5
if django.VERSION < (1, 7):
Expand Down
12 changes: 11 additions & 1 deletion polymorphic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,16 @@ def test_proxy_models(self):

self.assertIsInstance(items[0], ProxyChild)

def test_queryset_on_proxy_model_does_not_return_superclasses(self):
ProxyBase.objects.create(some_data='Base1')
ProxyBase.objects.create(some_data='Base2')
ProxyChild.objects.create(some_data='Child1')
ProxyChild.objects.create(some_data='Child2')
ProxyChild.objects.create(some_data='Child3')

self.assertEquals(5, ProxyBase.objects.count())
self.assertEquals(3, ProxyChild.objects.count())

def test_proxy_get_real_instance_class(self):
"""
The call to ``get_real_instance()`` also checks whether the returned model is of the correct type.
Expand All @@ -1020,7 +1030,7 @@ def test_proxy_get_real_instance_class(self):
self.assertEqual(pb.get_real_instance(), nonproxychild)
self.assertEqual(pb.name, name)

pbm = ProxyChild.objects.get(id=1)
pbm = NonProxyChild.objects.get(id=1)
self.assertEqual(pbm.get_real_instance_class(), NonProxyChild)
self.assertEqual(pbm.get_real_instance(), nonproxychild)
self.assertEqual(pbm.name, name)
Expand Down

0 comments on commit 120f124

Please sign in to comment.