基于多个模型的SearchIndex

基于多个模型的SearchIndex

本文介绍了Django Haystack - 基于多个模型的SearchIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的两个模型,我有一些问题,尝试在Django上用Haystack创建我的SearchIndex,我不知道该怎么做。

 #Meta:存储有关教程(类别,标题)的元数据
class Meta(models

数据库[tutorial.meta]

mta_title = models.CharField(max_length = TUTORIAL_TITLE_MAX)
mta_views =模型。 PositiveIntegerField(default = 0)


#内容:存储教程文本内容
类内容(models.Model):

数据库[tutorial.contents]

tut_id = IdField()
cnt_body = BBCodeTextField()

现在我想将我的SearchIndex作为3个以下字段:mta_title,mta_views和cnt_body。以下是我目前的SearchIndex:

从haystack导入索引
从tutorial.models导入Meta作为TutorialMeta
从account.models导入配置文件作为UserProfile


类TutorialMetaIndex(indexes.SearchIndex,indexes.Indexable):
text = indexes.CharField(document = True,use_template = True)
title = indexes.CharField(model_attr ='mta_title')
views = indexes.CharField(model_attr ='mta_views')
#Haystack保留内部字段名称内部使用
cnt_body = indexes.CharField()

def get_model(self):
return TutorialMeta

def index_queryset(self,using = None):
当模型的整个索引被更新时使用。
return self.get_model()。objects.all()

def prepare_cnt_body(self,obj):
????

我看过这个问题,答案是创建一个prepare_cnt_body。但是我不知道该怎么回来。



谢谢大家。

解决方案

谢谢Sectio Aurea,



但是这里是一个简单的准备功能的解决方案:

  class TutorialIndex(indexes.SearchIndex,indexes.Indexable):

索引教程

text = indexes .CharField(document = True,use_template = True)
tut_id = indexes.IntegerField(model_attr ='tut_id')
cnt_body = indexes.CharField(model_attr ='cnt_body')
mta_title =索引.CharField()
mta_views = indexes.CharField()

def get_model(self):

返回当前模型

return TutorialContents

def get_updated_field(self):

返回更新日期跟踪字段

return cnt_date

def index_queryset(self,usin g =无):

当模型的整个索引被更新时使用。

return self.get_model()。objects.all()

def prepare(self,object):

准备搜索数据

self.prepared_data = super(TutorialIndex,self).prepare(object)

#检索教程metas并返回准备的数据
meta = get_tutorial_meta(id = object.tut_id)
self.prepared_data ['mta_title'] = meta.mta_title
self.prepared_data ['mta_views'] = meta.mta_views

return self.prepared_data


I have some problems by trying to create my SearchIndex with Haystack on Django and I don't know what to do.

Here are my two models:

# Meta: stores meta data about tutorials (category, title)
class Meta(models.Model):
    """
    Database [tutorial.meta]
    """
    mta_title = models.CharField(max_length=TUTORIAL_TITLE_MAX)
    mta_views = models.PositiveIntegerField(default=0)


# Contents: stores the tutorial text content
class Contents(models.Model):
    """
    Database [tutorial.contents]
    """
    tut_id = IdField()
    cnt_body = BBCodeTextField()

Now I want to base my SearchIndex on the 3 following fields: mta_title, mta_views and cnt_body. Here are my current SearchIndex:

from haystack import indexes
from tutorial.models import Meta as TutorialMeta
from account.models import Profile as UserProfile


class TutorialMetaIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='mta_title')
    views = indexes.CharField(model_attr='mta_views')
    # Haystack reserves the content field names for internal use
    cnt_body = indexes.CharField()

    def get_model(self):
        return TutorialMeta

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all()

    def prepare_cnt_body(self, obj):
        ????

I've seen on this question, that the answer was to create a prepare_cnt_body. But I don't know what I should return.

Thank you everyone.

解决方案

Thank you Sectio Aurea,

But here is my solution with a simple prepare function:

class TutorialIndex(indexes.SearchIndex, indexes.Indexable):
    """
    Index the tutorials
    """
    text = indexes.CharField(document=True, use_template=True)
    tut_id = indexes.IntegerField(model_attr='tut_id')
    cnt_body = indexes.CharField(model_attr='cnt_body')
    mta_title = indexes.CharField()
    mta_views = indexes.CharField()

    def get_model(self):
        """
        Return the current model
        """
        return TutorialContents

    def get_updated_field(self):
        """
        Return the update date tracking field
        """
        return "cnt_date"

    def index_queryset(self, using=None):
        """
        Used when the entire index for model is updated.
        """
        return self.get_model().objects.all()

    def prepare(self, object):
        """
        Prepare the search data
        """
        self.prepared_data = super(TutorialIndex, self).prepare(object)

        # Retrieve the tutorial metas and return the prepared data
        meta = get_tutorial_meta(id=object.tut_id)
        self.prepared_data['mta_title'] = meta.mta_title
        self.prepared_data['mta_views'] = meta.mta_views

        return self.prepared_data

这篇关于Django Haystack - 基于多个模型的SearchIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 10:08