Created
June 8, 2012 00:11
-
-
Save cooncesean/2892533 to your computer and use it in GitHub Desktop.
How To Use Haystack to Add Results Based on a MultiValueField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Author(models.Model): | |
name = models.CharField(max_length=200, default='') | |
class Article(models.Model): | |
name = models.CharField(max_length=200, default='') | |
text = models.TextField(default='') | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from haystack import indexes | |
from myapp.models import Article | |
class ArticleIndex(indexes.Indexable): | |
name = indexes.CharField() | |
text = indexes.CharField(indexed=False, document=True) | |
authors = indexes.MultiValueField() | |
def get_model(self): | |
return Article | |
def prepare_authors(self, obj): | |
return [(author.name) for author in obj.authors.all()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from haystack.query import SearchQuerySet | |
term = 'foo' | |
# I know how to filter the results by author | |
SearchQuerySet().filter(name=term).filter(authors=term) | |
# How do I search BOTH the `name` and `authors` fields using the same term? | |
SearchQuerySet().filter(name=term) | SearchQuerySet().filter(authors=term) |
I know this is a veeery old one, but for future generations I will post the answer: use filter_or like this
SearchQuerySet().filter_or(name=term).filter_or(authors=term)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What search engine are you using as back-end i am using whoosh, It's not working for me, no results returned.