Fix regression with Django < 1.9
parent
8d53914344
commit
78d3cd4945
|
|
@ -8,7 +8,7 @@ import copy
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
import django
|
import django
|
||||||
from django.db.models.query import ModelIterable, QuerySet, Q
|
from django.db.models.query import QuerySet, Q
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
|
|
@ -25,26 +25,11 @@ except ImportError:
|
||||||
Polymorphic_QuerySet_objects_per_request = CHUNK_SIZE
|
Polymorphic_QuerySet_objects_per_request = CHUNK_SIZE
|
||||||
|
|
||||||
|
|
||||||
class PolymorphicModelIterable(ModelIterable):
|
def _polymorhic_iterator(queryset, base_iter):
|
||||||
"""
|
|
||||||
ModelIterable for PolymorphicModel
|
|
||||||
|
|
||||||
Yields real instances if qs.polymorphic_disabled is False,
|
|
||||||
otherwise acts like a regular ModelIterable.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
base_iter = super(PolymorphicModelIterable, self).__iter__()
|
|
||||||
if self.queryset.polymorphic_disabled:
|
|
||||||
return base_iter
|
|
||||||
return self._polymorhic_iterator(base_iter)
|
|
||||||
|
|
||||||
def _polymorhic_iterator(self, base_iter):
|
|
||||||
"""
|
"""
|
||||||
Here we do the same as::
|
Here we do the same as::
|
||||||
|
|
||||||
base_result_objects = list(super(PolymorphicModelIterable, self).__iter__())
|
real_results = queryset._get_real_instances(list(base_iter))
|
||||||
real_results = self.queryset._get_real_instances(base_result_objects)
|
|
||||||
for o in real_results: yield o
|
for o in real_results: yield o
|
||||||
|
|
||||||
but it requests the objects in chunks from the database,
|
but it requests the objects in chunks from the database,
|
||||||
|
|
@ -62,7 +47,7 @@ class PolymorphicModelIterable(ModelIterable):
|
||||||
reached_end = True
|
reached_end = True
|
||||||
break
|
break
|
||||||
|
|
||||||
real_results = self.queryset._get_real_instances(base_result_objects)
|
real_results = queryset._get_real_instances(base_result_objects)
|
||||||
|
|
||||||
for o in real_results:
|
for o in real_results:
|
||||||
yield o
|
yield o
|
||||||
|
|
@ -71,6 +56,25 @@ class PolymorphicModelIterable(ModelIterable):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
if django.VERSION >= (1, 9):
|
||||||
|
|
||||||
|
from django.db.models.query import ModelIterable
|
||||||
|
|
||||||
|
class PolymorphicModelIterable(ModelIterable):
|
||||||
|
"""
|
||||||
|
ModelIterable for PolymorphicModel
|
||||||
|
|
||||||
|
Yields real instances if qs.polymorphic_disabled is False,
|
||||||
|
otherwise acts like a regular ModelIterable.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
base_iter = super(PolymorphicModelIterable, self).__iter__()
|
||||||
|
if self.queryset.polymorphic_disabled:
|
||||||
|
return base_iter
|
||||||
|
return _polymorhic_iterator(self.queryset, base_iter)
|
||||||
|
|
||||||
|
|
||||||
def transmogrify(cls, obj):
|
def transmogrify(cls, obj):
|
||||||
"""
|
"""
|
||||||
Upcast a class to a different type without asking questions.
|
Upcast a class to a different type without asking questions.
|
||||||
|
|
@ -110,6 +114,7 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(PolymorphicQuerySet, self).__init__(*args, **kwargs)
|
super(PolymorphicQuerySet, self).__init__(*args, **kwargs)
|
||||||
|
if django.VERSION >= (1, 9):
|
||||||
self._iterable_class = PolymorphicModelIterable
|
self._iterable_class = PolymorphicModelIterable
|
||||||
# init our queryset object member variables
|
# init our queryset object member variables
|
||||||
self.polymorphic_disabled = False
|
self.polymorphic_disabled = False
|
||||||
|
|
@ -454,6 +459,20 @@ class PolymorphicQuerySet(QuerySet):
|
||||||
|
|
||||||
return resultlist
|
return resultlist
|
||||||
|
|
||||||
|
if django.VERSION < (1, 9):
|
||||||
|
# Before Django 1.9 ModelIterable functionality was implemented in `iterator` method
|
||||||
|
def iterator(self):
|
||||||
|
base_iter = super(PolymorphicQuerySet, self).iterator()
|
||||||
|
|
||||||
|
# disabled => work just like a normal queryset
|
||||||
|
if self.polymorphic_disabled:
|
||||||
|
for o in base_iter:
|
||||||
|
yield o
|
||||||
|
return
|
||||||
|
|
||||||
|
for o in _polymorhic_iterator(self, base_iter):
|
||||||
|
yield o
|
||||||
|
|
||||||
def __repr__(self, *args, **kwargs):
|
def __repr__(self, *args, **kwargs):
|
||||||
if self.model.polymorphic_query_multiline_output:
|
if self.model.polymorphic_query_multiline_output:
|
||||||
result = [repr(o) for o in self.all()]
|
result = [repr(o) for o in self.all()]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue