Reputation: 173
I have a list of querysets as follows. Is there a way to merge all these query sets of a list into one query set ? The queryset elements of this list are dynamically determined. As in the example below it is three querysets in the list. In next hypothetical iteration it could be 40 querysets.
capture_list =[<QuerySet [<Fields_Model: Fields_Model object (11)>]>,<QuerySet [<Fields_Model: Fields_Model object (12)>]>,<QuerySet [<Fields_Model: Fields_Model object (13)>]>]
Upvotes: 2
Views: 1057
Reputation: 476557
You can union these with:
Fields_Model.objects.none().union(*capture_list)
Here the asterisk will thus "unpack" the querysets as individual parameters for the .union(…)
call [Django-doc].
A call with .union(…)
will only select distinct objects, so no duplicates. If you want to obtain the duplicates, you can the all=True
parameter:
Fields_Model.objects.none().union(*capture_list, all=True)
Upvotes: 3