从Haystack / whoosh中的两个模型中获得结果

时间:2014-10-31 06:37:17

标签: django django-haystack

我在模型上使用了Haystack并且工作得很好。但是当我尝试使用两个模型时,它会一直返回无。

这些是我的模特:

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

这些是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()

这是观点:

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
    })

以下是表格:

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

如果其他文件有帮助,我也会提供。

如何从两个模型中搜索?

P.S。它适用于Note,但不适用于People

0 个答案:

没有答案