[Answered ]-Django Q Filter โ€“ Too many Values to unpack

1๐Ÿ‘

โœ…

You should not work with ={2}. A Q object is not a string of the form foo=bar, it is in essence a 2-tuple with as first element a string that is the "key" which is the field and optionally some lookups, and as second element a value, but that value can be a string, datetime object, etc. You thus construct the Q object with:

if i[0] == '%' and i[-1] == '%':
    result.add(Q((f'{key}__contains', i[1:-1])), Q.OR)
elif i[0] == '%' and not i[-1] == '%':
    result.add(Q((f'{key}__startswith', i[1:])), Q.OR)
elif not i[0] == '%' and i[-1] == '%':
    result.add(Q((f'{key}__endswith', i[:-1])), Q.OR)
else:
    result.add(Q((key, i)), Q.OR)

Leave a comment