Fix missing subclasses and annotated in cloned InheritanceQueryset (#335)

Bug only occurs in django > 2
This commit is contained in:
Sebastian Illing 2018-12-08 06:26:36 +01:00 committed by Asif Saif Uddin
parent 2cb773372d
commit 326bd7bc02
2 changed files with 9 additions and 1 deletions

View file

@ -97,7 +97,11 @@ class InheritanceQuerySetMixin(object):
def _clone(self, klass=None, setup=False, **kwargs):
if django.VERSION >= (2, 0):
return super(InheritanceQuerySetMixin, self)._clone()
qs = super(InheritanceQuerySetMixin, self)._clone()
for name in ['subclasses', '_annotated']:
if hasattr(self, name):
setattr(qs, name, getattr(self, name))
return qs
for name in ['subclasses', '_annotated']:
if hasattr(self, name):

View file

@ -460,3 +460,7 @@ class InheritanceManagerRelatedTests(InheritanceManagerTests):
qs = InheritanceManagerTestParent.objects.annotate(
test_count=models.Count('id')).select_subclasses()
self.assertEqual(qs.get(id=self.child1.id).test_count, 1)
def test_clone_when_inheritance_queryset_selects_subclasses_should_clone_them_too(self):
qs = InheritanceManagerTestParent.objects.select_subclasses()
self.assertEqual(qs.subclasses, qs._clone().subclasses)