def __iter__(self):
        pk = self.queryset.model._meta.pk
        if not isinstance(pk, (IntegerField, AutoField)):
            for result in iter(IterableQuerySetWrapper(self.queryset, self.step, self.limit)):
                yield result
        else:
            max_id = self.max_id
            if self.min_id is not None:
                at = self.min_id
            elif not self.sorted:
                at = 0
            else:
                at = None

            num = 0
            limit = self.limit or max_id

            if isinstance(self.queryset, RangeQuerySet):
                extra_kwargs = {'bypass': True}
            else:
                extra_kwargs = {}

            has_results = True
            while ((max_id and at <= max_id) or has_results) and (not self.limit or num < self.limit):
                start = num

                if at is None:
                    results = self.queryset
                elif self.desc:
                    results = self.queryset.filter(id__lte=at)
                elif not self.desc:
                    results = self.queryset.filter(id__gte=at)

                # Adjust the sort order if we're stepping through reverse
                if self.sorted:
                    if self.desc:
                        results = results.order_by('-id')
                    else:
                        results = results.order_by('id')

                if self.max_id:
                    results = results.filter(id__lte=max_id)

                results = results[:self.step].iterator(**extra_kwargs)
                if self.select_related:
                    # we have to pull them all into memory to do the select_related
                    results = list(results)
                    for fkey in self.select_related:
                        if '__' in fkey:
                            fkey, related = fkey.split('__')
                        else:
                            related = []
                        attach_foreignkey(results, getattr(self.queryset.model, fkey, related))

                if self.callbacks:
                    results = list(results)
                    for callback in self.callbacks:
                        callback(results)

                for result in results:
                    yield result
                    num += 1
                    at = result.id
                    if (max_id and result.id >= max_id) or (limit and num >= limit):
                        break

                if at is None:
                    break

                has_results = num > start
                if self.desc:
                    at -= 1
                else:
                    at += 1
dataset/ETHPy150Open disqus/django-db-utils/dbutils/querysets.py/RangeQuerySetWrapper.__iter__/original