问题
I used Haystack with on model and it worked just fine. But when I tried to use two models, it keeps returning None.
These are my models:
class People(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __unicode__(self):
return self.name
class Note(models.Model):
user = models.ForeignKey(CustomUser)
title = models.CharField(max_length=200)
body = models.TextField()
pub_date = models.DateTimeField()
def __unicode__(self):
return self.title
These are the classes in search_indexes.py:
class NoteIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
body = indexes.CharField(model_attr='body')
pub_date = indexes.DateTimeField(model_attr='pub_date')
def get_model(self):
return Note
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(
pub_date__lte=datetime.datetime.now())
class PeopleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
full_name = indexes.CharField(model_attr='name')
desc = indexes.CharField(model_attr='description')
def get_model(self):
return People
def index_queryset(self, using=None):
return self.get_model().objects.all()
This is the view:
def root(request):
form = NoteSearchForm(request.GET)
form2 = PeopleSearchForm(request.GET)
results = form.search()
results2 = form2.search()
return render(request, 'search/search.html', {
'notes': results,
'people': results2
})
Here are the forms:
class NoteSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
sqs = super(NoteSearchForm, self).search()
if not self.is_valid():
return self.no_query_found()
# sqs = sqs.order_by('title')
return sqs
class PeopleSearchForm(SearchForm):
def no_query_found(self):
return self.searchqueryset.all()
def search(self):
sqs = super(PeopleSearchForm, self).search().models(Note, People)
if not self.is_valid():
return self.no_query_found()
return sqs
If other files help, I will provide them too.
How do I search from two models?
P.S. It is working for Note, but not for People
来源:https://stackoverflow.com/questions/26668796/get-results-from-two-models-in-haystack-whoosh