3

我試圖讓Django的草垛這裏(使用Xapian的後端)索引我的模型搜索,由namedescription領域。在django-haystack中,我如何使用模型的子類?

我有一個Item,Device的子類,它增加了一個manufacturer字段。

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的乾草堆的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使得manufacturer場編入索引,當且僅當模型對象是Device一個實例?

回答

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

...草堆教程是有點混亂對這個問題(你實際上並沒有使用文本文件模板,爲一個),但其基本思想是,草堆的引擎去鎮上無論文本數據在這個模板中。

實際上,它去鎮上無論是在你發送的響應,但如果你已經有了模板設置,你可以使用你在那裏想要的任何Django的模板邏輯。 (請注意,在Django 1.2之前,if模板標籤有點像是狗的早餐;如果您被困在較早的Django版本中,您可能需要調整語法,但原理相同。)

+0

謝謝:)這並獲得成功。 – 2010-10-19 23:03:30

+0

很高興聽到它。 – fish2000 2010-10-19 23:35:47

相關問題