0

I'm building my filters as a list but having trouble building them with the Q function. It seems to be that doing things manually the filters work but when trying to build up the filters by concatenating strings I get the following issues:

Here is the Query:

MyLocationFilter = BuildQueryORFilter('MyLocationCountryCode', MyLocationCodePref1)
list = AboutMe.objects.order_by('MyLinkedInLastName').filter(reduce(OR, MyLocationFilter))

And here is how I am building those Filters:

def BuildQueryORFilter(fieldname, fieldvalues):
    QueryList = []
    spltfieldvalues = fieldvalues.split()
    for item in spltfieldvalues:
        strpitem = item.strip('[],')
        queryitem = Q(fieldname+"__contains="+strpitem)
        QueryList.append(queryitem)
    return QueryList

However the problem seems to be how to get Q(..) in the form of Q(fieldname__contains=gb) rather than Q('fieldname__contains=gb') which seems to be throwing up issues such as:

ValueError
Exception Value:    
need more than 1 value to unpack

How should I build the Q queries to avoid this unpacking issue?

Traceback for possible answer (below)

Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/brett/LinkedIn/phaseone/jelt/views.py" in AboutMeList
  259.  list = AboutMe.objects.order_by('MyLinkedInLastName').filter(MyLocationFilter)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in filter
  624.         return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py" in _filter_or_exclude
  642.             clone.query.add_q(Q(*args, **kwargs))
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_q
  1250.                             can_reuse=used_aliases, force_having=force_having)
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/query.py" in add_filter
  1056.         arg, value = filter_expr

Exception Type: ValueError at /list/
Exception Value: need more than 1 value to unpack

1 Answer 1

2

Pass the keyword argument by unpacking a dict.

    strpitem = item.strip('[],')
    key = fieldname + "__contains"
    d = {key: strpitem}
    queryitem = Q(**d)
Sign up to request clarification or add additional context in comments.

10 Comments

I tied this, but when printing out queryitem to screen I get: (AND: ('MyLocationCountryCode__contains', u'gb')) and I'm still getting the same issue when trying to filter.
Is that the traceback when you use the answer I suggested? What is the result of printing the dictionary d? There might be a typo in my suggestion, or another mistake in your question that I haven't spotted, but it's definitely closer to the correct solution. It is not possible to pass a single string argument toQ().
printing the dictionary: Content-Type: text/html; charset=utf-8 {'MyLocationCountryCode__contains': u'gb'}
I can't seem to find a smart way to unpack that dictionary
I can't spot any other bugs. I would start with something really basic like d={'MyLocationCountryCode__contains': 'gb'} and AboutMe.objects.filter(Q(**d)), then build up the functionality of your method from there. Good luck!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.