在 django-haystack 中,如何使用模型的子类?
我试图让 django-haystack (使用 xapian 后端)通过 name
和 description
字段在此处索引我的模型以进行搜索。
我有一个 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-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
添加什么,以便为 manufacturer
字段建立索引设备
?
I'm trying to get django-haystack (using a xapian backend) to index my model here for search, by the name
and description
fields.
I have a subclass of Item
, Device
, which adds a manufacturer
field.
The Item
model is defined thusly:
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)
My subclass of django-haystack’s SearchIndex
looks like this:
class ItemIndex(SearchIndex):
text = CharField(document=True, use_template=True)
name = CharField(model_attr='name')
description = CharField(model_attr='description')
site.register(Item, ItemIndex)
I have set up this template, in templates/search/indexes/catalog/item_text.txt
:
{{ object.name }}
{{ object.description }}
What do I add to item_text.txt
such that the manufacturer
field gets indexed, if and only if the model object is an instance of Device
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
... Haystack 教程在这个主题上有点令人困惑(例如,您实际上不必使用文本文件模板),但基本思想是 Haystack 的引擎会根据该模板中的任何文本数据进行处理。
...实际上,它会根据您发送的响应中的内容进行处理,但是如果您已经设置了模板,则可以使用您想要的任何 Django 模板逻辑。
(请注意,
if
模板标签在 Django 1.2 之前有点像狗的早餐;如果你坚持使用早期的 Django 版本,你可能需要调整语法,但原理是相同的.)... the Haystack tutorial is a bit confusing on this subject (you don't actually have to use a text-file template, for one) but the basic idea is that Haystack's engine goes to town on whatever text data is in this template.
... Actually, it goes to town on whatever is in the response you send it, but if you've got the template set up you can use whatever Django template logic you want in there.
(note that the
if
template tag was a bit of a dog's breakfast prior to Django 1.2; if you're stuck on an earlier Django version you may have to tweak the syntax, but the principle is the same.)