Skip to content

Instantly share code, notes, and snippets.

@ilebedie
Created August 14, 2018 14:22
Show Gist options
  • Save ilebedie/ad41c1d7e0145d9916fab2d42c75cda9 to your computer and use it in GitHub Desktop.
Save ilebedie/ad41c1d7e0145d9916fab2d42c75cda9 to your computer and use it in GitHub Desktop.
Combine django querysets
def chain_qs(*querysets):
class CombinedQuerySet:
def __init__(self, *querysets):
self.qs_list = querysets
def __next__(self):
return chain(self.qs_list)
def __iter__(self):
return self
next = __next__
def __len__(self):
return sum(len(q) for q in self.qs_list)
return CombinedQuerySet(querysets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment