django haystack和solr在模型中搜索两个类

时间:2016-06-04 08:34:53

标签: django solr django-haystack

每一个我都是django haystack和solr的新手,我现在可以使用solr来搜索一个模型。但是,当它出现两个模型时我不知道该怎么做? 这是我的

  

models.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from taggit.managers import TaggableManager

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super(PublishedManager, self).get_queryset().filter(status='published')


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

    objects = models.Manager() # The default manager.
    published = PublishedManager() # The Dahl-specific manager.

    tags = TaggableManager()

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail', args=[self.publish.year,
                                                 self.publish.strftime('%m'),
                                                 self.publish.strftime('%d'),
                                                 self.slug])


class Comment(models.Model):
    post = models.ForeignKey(Post, related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return 'Comment by {} on {}'.format(self.name, self.post)

如果我使用这些search_indexes和post_text.txt,我可以搜索Post确定

  

search_indexes.py

from haystack import indexes
from .models import Post,Comment


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    publish = indexes.DateTimeField(model_attr='publish')

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().published.all()
  

mysite的/博客/模板/搜索/索引/博客/ post_text.txt

{{ object.title }}
{{ object.tags.all|join:", " }}
{{ object.body }}

但是,我还必须搜索Comment

然后我尝试这些,他们没有工作

  

search_indexes.py

from haystack import indexes
from .models import Post,Comment


class PostIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    publish = indexes.DateTimeField(model_attr='publish')

    def get_model(self):
        return Post

    def index_queryset(self, using=None):
        return self.get_model().published.all()


class Comment(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    comments = indexes.DateTimeField(model_attr='comments')

    def get_model(self):
        return Comment
  

mysite的/博客/模板/搜索/索引/博客/ comment_text.txt

{{ object.name }}
{{ object.email }}
{{ object.body }}
  

views.py

.......

def post_search(request):
    form = SearchForm()
    if 'query' in request.GET:
        form = SearchForm(request.GET)
        if form.is_valid():
            cd = form.cleaned_data
            results = SearchQuerySet().models(Post).filter(content=cd['query']).load_all()
            # count total results
            total_results = results.count()
        return render(request, 'blog/post/search.html', {'form': form,
                                                         'cd': cd,
                                                         'results': results,
                                                         'total_results': total_results})



    return render(request, 'blog/post/search.html', {'form': form,})

所以我应该添加更多views.py搜索并更改search_indexes.py和post_text.txt ??

0 个答案:

没有答案