I have to merge the querysets below in a single list:
result_list_1 = Col.objects.defer("tags").filter(producer__username__icontains=crit)
result_list_2 = Col.objects.defer("tags").filter(name__icontains=crit)
result_list_3 = Col.objects.defer("tags").filter(description__icontains=crit)
result_list_4 = Col.objects.filter(tags__name__icontains=crit)
...
Each result_list contains items, which have a unique numeric id I can use to make sure there are no dups.
I can't use | while querying the DB or Q objects.
How do I merge the resulsets in one single list?
What about a slight modification of itertools.chain
that makes sure you don't get dupes:
def unique_chain(*iterables):
known_ids = set()
for it in iterables:
for element in it:
if element.id not in known_ids:
known_ids.add(element.id)
yield element
With that you can create your combined list:
combined_list = list(unique_chain(result_list_1, result_list_2, ... ))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With