3

django-haystack(xapianバックエンドを使用)を取得して、ここでモデルにインデックスを付けて、フィールドnamedescriptionフィールドで検索しようとしています。

フィールドを追加する、Itemのサブクラスがあります。Devicemanufacturer

モデルは次のItemように定義されます。

class Item(models.Model):
    name = models.CharField(max_length=255, unique=True)
    description = models.TextField(null=True, blank=True)
    compatible_with = models.ManyToManyField('self', null=True, blank=True)
    often_with = models.ManyToManyField('self', null=True, blank=True)
    created_by = models.ForeignKey(User, null=True, blank=True, related_name='created_by')
    verified = models.BooleanField(default=False)
    verified_by = models.ForeignKey(User, null=True, blank=True, related_name='verified_by')
    date_created = models.DateField(auto_now_add=True)
    slug = models.SlugField(max_length=300, null=True, blank=True)

django-haystackの私のサブクラスは次のSearchIndexようになります。

class ItemIndex(SearchIndex):
    text = CharField(document=True, use_template=True)
    name = CharField(model_attr='name')
    description = CharField(model_attr='description')

site.register(Item, ItemIndex)

私はこのテンプレートを次のように設定しましたtemplates/search/indexes/catalog/item_text.txt

{{ object.name }}
{{ object.description }}

モデルオブジェクトがのインスタンスである場合にのみ、フィールドにインデックスが付けられるitem_text.txtように何を追加しますか?manufacturerDevice

4

1 に答える 1

3
{% if device.manufacturer %}
{{ device.manufacturer }}
{% endif %}

... Haystackチュートリアルは、このテーマに関して少し混乱しています(たとえば、実際にはテキストファイルテンプレートを使用する必要はありません)が、基本的な考え方は、Haystackのエンジンがこのテンプレートに含まれるテキストデータに基づいて町に行くということです。 。

...実際には、送信した応答に含まれるものはすべて町に送られますが、テンプレートを設定している場合は、そこで必要なDjangoテンプレートロジックを使用できます。

ifテンプレートタグは、Django 1.2より前の犬の朝食でした。以前のバージョンのDjangoに固執している場合は、構文を微調整する必要があるかもしれませんが、原則は同じです。)

于 2010-10-16T13:08:01.930 に答える