How to use SearchVector with multiple fields from a list of strings
<p>I don't have a lot of Python experience and can't figure out how to use a list of strings with the SearchVector functionality described <a href="https://docs.djangoproject.com/en/1.10/ref/contrib/postgres/search/#searchvector" rel="nofollow">here</a>. I'm using Django 1.10 with PostgreSQL 9.6 and have verified that this works if I write the fields in manually. </p>

<p>This function is from a class based view and its purpose is to receive a string from a form, then do a full text search against all CharFields in all Models within MyApp, then return the results (planning on using <a href="http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view">itertools</a> to combine multiple QuerySets once I get this working). I don't want to hardcode the fields because we will be adding new objects and new fields regularly - its a sort of corporate middleware app where I am not able to master the data. </p>

<pre><code>def get_queryset(self):
    searchTargets = defaultdict(list)
    myModels = (obj for obj in apps.get_models()
                if obj._meta.label.startswith('myapp')
                and not obj._meta.label == 'myapp.ConfigItem')
    for thing in myModels:
        fieldNames = (x.name for x in thing._meta.fields)
        for thisField in fieldNames:
            if thing._meta.get_field(thisField).get_internal_type() == 'CharField':
                searchTargets[thing._meta.object_name].append(thisField)
    user_query=self.request.GET['user_query']
    for key in searchTargets.iterkeys():
        targetClass=class_for_name('myapp.models',key)
        results = targetClass.objects.annotate(
            search=SearchVector(searchTargets[key]),
            ).filter(search=user_query)
        #only a single QuerySet will be returned. 
        return results
</code></pre>

<p>I've tried ','.join(MyList) but of course that just makes one big string. </p>

<p>Expand the list <a href="https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">using *</a> and SearchVector will happily search multiple fields. </p>

<pre><code>results = targetClass.objects.annotate(
    search=SearchVector(*searchTargets[key]),
    ).filter(search=user_query)
</code></pre>

python django postgresql