При сортировке цепочечного набора запросов объект 'itertools.islice' не имеет атрибута 'next'

Спасибо этой статье

https://stackoverflow.com/questions/431628/how-can-i-combine-two-or-more-querysets-in-a-django-view#:~:text=Use%20union%20operator%20for%20queryset,querysets%20by%20using%20union%20operator.&text=One%20other%20way%20to%20achieve,to%20use%20itertools%20chain%20function.

Я использую класс QuerySetChain для конкатенации нескольких queryset

class QuerySetChain(object):
    """
    Chains multiple subquerysets (possibly of different models) and behaves as
    one queryset.  Supports minimal methods needed for use with
    django.core.paginator.
    """

    def __init__(self, *subquerysets):
        self.querysets = subquerysets

    def count(self):
        """
        Performs a .count() for all subquerysets and returns the number of
        records as an integer.
        """
        return sum(qs.count() for qs in self.querysets)

    def _clone(self):
        "Returns a clone of this queryset chain"
        return self.__class__(*self.querysets)

    def _all(self):
        "Iterates records in all subquerysets"
        return chain(*self.querysets)
  
    def __getitem__(self, ndx):
        """
        Retrieves an item or slice from the chained set of results from all
        subquerysets.
        """
        if type(ndx) is slice:
            return list(islice(self._all(), ndx.start, ndx.stop, ndx.step or 1))
        else:
            return islice(self._all(), ndx, ndx+1).next()

Теперь я собираю несколько столов

    md = sm.Message.history.all()
    sw = sm.Worker.history.all()
    st = sm.Template.history.all()
    gp = sm.GlobalParam.history.all()


    matches = QuerySetChain(md, sw, st,gp) # it makes one queryset successfully

    result_list = sorted( #error occurs here
        matches,
        key=lambda instance: instance.updated_at)

Когда я пытаюсь отсортировать элементы, возникает ошибка, как показано ниже.

'itertools.islice' object has no attribute 'next' when sorting chained object

Возможно ли это отсортировать?

Вернуться на верх