Skip to content

Instantly share code, notes, and snippets.

@stopthink
Created December 1, 2010 21:38
Show Gist options
  • Select an option

  • Save stopthink/724273 to your computer and use it in GitHub Desktop.

Select an option

Save stopthink/724273 to your computer and use it in GitHub Desktop.
Combine multiple querysets from different models and sort by shared column (which can be fed into paginator)
from itertools import chain
from operator import attrgetter
collection = Collection.objects.get(pk=id)
document_list = collection.documents.all()
task_list = collection.tasks.all()
item_list = sorted(
chain(document_list, task_list),
key=attrgetter('created'),
reverse=True
)
paginator = Paginator(item_list, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment