TomTom
TomTom

Reputation: 1163

Django Q set too many values to unpack

I am new to django and trying to filter multiple fields that contain text.

columns = ['ticketId', 'checkSum']
q_objects = [Q(fieldname +'__contains', myString) for fieldname in columns]
objects = objects.filter(reduce(operator.or_, q_objects))

I get Exception Type: ValueError Exception Value: too many values to unpack on the "filter" last line. Any ideas

Upvotes: 0

Views: 1465

Answers (2)

joechild
joechild

Reputation: 111

Try this:

Q(**{fieldname + '__contains': myString})

This is equivalent to providing a keyword argument, as you normally would when instantiating a Q object. For example:

Q(fieldname__contains=myString, another_fieldname__contains=myOtherstring)

The Q object essentially needs pairs of values to work. Looking at the code it seems you can also use tuples of length two, like this (I haven't tested, though):

Q(("fieldname__contains", myString), ("another_fieldname__contains", myOtherString))

Upvotes: 1

dursk
dursk

Reputation: 4445

What is the model you are querying? It looks like you left that out.

The last line,

objects = objects.filter(reduce(operator.or_, q_objects))

Should be something like

objects = MyModel.objects.filter(...)

Upvotes: 0

Related Questions