Skip to content

Commit

Permalink
Merge pull request #210 from andrewdodd/IncludeProxyModelOnly
Browse files Browse the repository at this point in the history
Issue #200 - Exclude super-class from proxy model query sets
  • Loading branch information
vdboor committed May 23, 2016
2 parents 75646f1 + 120f124 commit 1c4face
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
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
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 1c4face

Please sign in to comment.