Created
August 14, 2018 14:22
-
-
Save ilebedie/ad41c1d7e0145d9916fab2d42c75cda9 to your computer and use it in GitHub Desktop.
Combine django querysets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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