python - How to do multiple exclude commands using itertools.chain in Django? -
suppose wanna run exclude command repeatedly getting variables exclude_list e.g. ['aa', 'ab' 'ac'].
i can using loop:
for exclude_value in exclude_list: myqueryset.exclude(variable__startswith=exclude_value) however, i'd using itertools.chain command i've read capable of doing so. suggestions?
what doing correct approach - except 1 small detail, aren't retaining excludes. django querysets lazily evaluated, running through loop , continually chaining won't anything, right until try access set.
if this:
qs = mymodel.objects exclude_value in exclude_list: qs = qs.exclude(variable__startswith=exclude_value) qs = none the database never hit.
so this:
qs = mymodel.objects exclude_value in exclude_list: qs = qs.exclude(variable__startswith=exclude_value) qs.count() # or whatever want queryset and should fine, if/when experience database slowdown, ill because of large number of freetext expressions in query, profiling, can find efficiency.
but i'd wager above code sufficient needs.
Comments
Post a Comment