Reputation: 449
I come across this syntax on stackoverflow
from itertools import chain
result_list = list(chain(page_list, article_list, post_list))
I need to concatenate a bunch of QuerySets with something like this:
prjExpList = list(chain(lvl for lvl in prjlvl))
prjEnvList = list(chain(env for env in prjEnv))
This gives me an error of
AttributeError: 'QuerySet' object has no attribute '_meta'
My goal is to concatenate a bunch of QuerySets that's stored inside a list prjlvl
and prjEnv
How do I do that?
Upvotes: 1
Views: 1121
Reputation: 4495
Are the QuerySets of the same model? You can just do
combined_queryset = queryset_1 | queryset_2 | queryset_3
To chain them together into one QuerySet. This means you can still do things with the QuerySet in the ORM, which is a big help.
Upvotes: 2
Reputation: 9046
Try:
prjExpList = list(chain.from_iterable(prjlvl))
Note that itertools.chain()
takes a *args
list of iterables, and that you can get the same unpacking behavior from the chain.from_iterable()
class method.
See: https://docs.python.org/2/library/itertools.html#itertools.chain
Upvotes: -1