What to do when searching on more than one word with django filter
<p>I made a filter where you can search on different keywords and it works. </p>

<p>My problem is that when i am trying to search on more than one keyword. </p>

<p>How do I make it possible to filter the search so it can separate each word?</p>

<p>Here is a picture how it looks:</p>

<p>First picture shows when i only search on one key word, and second shows when i search on two: <a href="http://i.stack.imgur.com/tGXwu.png" rel="nofollow"><img src="http://i.stack.imgur.com/tGXwu.png" alt="enter image description here"></a> 
<a href="http://i.stack.imgur.com/JAzmH.png" rel="nofollow"><img src="http://i.stack.imgur.com/JAzmH.png" alt="enter image description here"></a></p>

<p>Here is my code for The model class: </p>

<pre><code>class Task(managers.Model):
keywords = models.ManyToManyField('Keyword', blank=True, related_name='event_set')
objects = managers.DefaultSelectOrPrefetchManager.from_queryset(managers.TaskQuerySet)()
</code></pre>

<p>And here is my Filter class: </p>

<pre><code>class TaskFilterSet(BaseFilterSet):

keywords = django_filters.MethodFilter(action="filter_keywords")

class Meta:
    model = models.Task

def filter_keywords(self, queryset, value):
    from django.db.models import Q
    return queryset.filter(Q(keywords__word__icontains=value))
</code></pre>

<p>Lets suppose you are searching for the following Keywords: <code>foo</code> and <code>boo</code>. And you have the following relation:</p>

<pre><code>search['foo','boo']
object.keywords['foo','boo','woo']
</code></pre>

<p>You can iterate over <code>object.keywords</code> and see if one of the search objects you have matches one of the <code>keywords</code> in <code>object</code>. If it does, you return the <code>filter</code> containing that object.</p>

python regex django django-filters