ValueError: too many values to unpack, passing a list as *args

252 views Asked by At

I have a problem passing arguments through a list to Django filter. Here my code:

args = [ "Q( title__icontains = 'Foo' ) | Q( author__icontains = 'Foo' )", "Q( title__icontains = 'Bar' ) | Q( author__icontains = 'Bar' )" ]
entries = Book.objects.filter( *args )

but filter returns this error:

ValueError: too many values to unpack,

2

There are 2 answers

0
Bernhard On BEST ANSWER

Your args are strings, but they must be Q objects. Remove the quotes around your Q object definitions.

0
giaosudau On

Remove all " and you get it right.

args = [ Q( title__icontains = 'Foo' ) | Q( author__icontains = 'Foo' ), Q( title__icontains = 'Bar' ) | Q( author__icontains = 'Bar' )]
entries = Book.objects.filter( *args )