Django Haystack 添加额外的搜索输入
我的干草堆搜索目前可以很好地搜索以下模型:
class AdminEntry(models.Model):
product = models.ForeignKey('Product')
number_entries = models.IntegerField(max_length=3, null=True)
我的 search_indexes.py
:
class AdminEntryIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='product__author')
title = CharField(model_attr='product__title')
desc = CharField(model_attr='product__desc')
def get_queryset(self):
return AdminEntry.objects.all()
site.register(AdminEntry, AdminEntryIndex)
但现在我想在我的搜索表单的下拉列表中添加额外的搜索参数,其中包含 2 个值 [' Admin', 'Staff']
因为我添加了另一个模型:
class StaffEntry(models.Model):
product = models.ForeignKey('Product')
number_entries = models.IntegerField(max_length=3, null=True)
如果所选的下拉列表是“Staff”,我希望我的搜索在 StaffEntry
上搜索,并且 AdminEntry< /代码>是选择“管理员”。有人可以帮助我如何使用 Haystack 和 Whoosh 来实现这一目标吗?提前致谢。
I've a haystack search currently works well searching on the following model:
class AdminEntry(models.Model):
product = models.ForeignKey('Product')
number_entries = models.IntegerField(max_length=3, null=True)
My search_indexes.py
:
class AdminEntryIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='product__author')
title = CharField(model_attr='product__title')
desc = CharField(model_attr='product__desc')
def get_queryset(self):
return AdminEntry.objects.all()
site.register(AdminEntry, AdminEntryIndex)
but now I want to add additional search parameter in dropdown in my search form with 2 values ['Admin', 'Staff']
since I've added another model:
class StaffEntry(models.Model):
product = models.ForeignKey('Product')
number_entries = models.IntegerField(max_length=3, null=True)
I want to my search to search on StaffEntry
if the dropdown selected is 'Staff', and AdminEntry
is 'Admin' is selected. Can someone help me on how to achieve this using Haystack with Whoosh? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要的是 ModelSearchForm:
http://django-haystack。 readthedocs.org/en/latest/views_and_forms.html#modelsearchform
使用此表单而不是默认的 SearchForm,您将获得每个索引模型的复选框。如果扩展表单,您将能够将其更改为选择而不是复选框。
请参阅http://django-haystack.readthedocs.org/en/latest/ views_and_forms.html#views 了解有关如何在视图中使用自定义表单的信息。
What you're after is the ModelSearchForm:
http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#modelsearchform
use this instead of the default SearchForm, and you'll get checkboxes for each indexed model. If you extend the form you'll be able to change it to a select instead of checkboxes.
See http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#views for information on how to use your custom form in the view.
forms.py
类搜索(SearchForm):
forms.py
class Search(SearchForm):