[Answered ]-Is it possible to Combine querysets from different models into a single queryset or a list and order by date

1👍

You can work with four queries and then merge these in the same list:

from operator import attrgetter

klasses = [Issue, Receive, SalesReturn]
customer = None  # some customer
result = sorted(
    [
        m
        for klass in klasses
        for m in klass.objects.filter(customer=customer).order_by('date_time')
    ],
    key=attrgetter('date_time'),
)

The .order_by('date_time') clause is not required, but Timsort often performs better if the data is already partly ordered, so we can (slightly) boost the process.

I also tries using sort() but it keeps returning None.

Sort always returns None: it does not return a sorted list, it modifies the order of the list, so after the .sort() call, x is sorted.

Leave a comment