I would return an iterator from the items method. in this case, sorting and aggregation of objects can be done in a lazy way. If you pre-sort the three collections of objects that you want to combine before creating an iterator, the final sort is just a matter of choosing the next object from the desired collection at each iteration. Do you understand what I mean?
eg:
class SiteFeed(Feed): ... def items(self): i = model1.objects.order_by('timestamp').iterator() j = model2.objects.order_by('timestamp').iterator() k = model3.objects.order_by('timestamp').iterator()
try: u = i.next() except StopIteration: u = None
try: v = j.next() except StopIteration: v = None
try: w = k.next() except StopIteration: w = None ... yield min([u,v,w], key=lambda x: x.timestamp if x else datetime.max) ...
source share