我有这两个模型:
class Shop(CustomBaseModel):
username = models.CharField(max_length=200)
bio = models.CharField(max_length=200)
class Item(CustomBaseModel):
shop = models.ForeignKey(Shop)
tags = models.TextField()
我在 search_indexes.py 中有这个索引:
class ItemIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
tags = indexes.CharField(model_attr='tags')
这个 item_text.txt 文件:
{{ object.tags}}
这是我的观点:
def search(request):
form = ItemSearchForm(request.GET)
results = form.search()
return render(request, 'search/search.html', {
'items': results
})
这是我的表格:
class ItemSearchForm(SearchForm):
def search(self):
sqs = super(ItemSearchForm, self).search().models(Item, Shop)
return sqs
这是模板:
{% if items|length > 0 %}
<h1>Items</h1>
{% for item in items %}
<p>
{{ item.tags }}
</p>
{% endfor %}
{% endif %}
这是从标签中搜索并且工作正常。但是,如果有人包含该商店的用户名或简介,我如何显示属于该商店的所有商品?
最佳答案
当使用Haystack并且您获得结果查询集时,您有一个对象列表,该对象的字段取决于您所写入的内容:
templates/search/indexes/project_name/modelName_text.txt
但是总有一个字段可以为您提供帮助,即字段 object
如果我理解你想知道的问题:
- 当用户使用用户名或个人简介查找商店时,如何显示商店的所有商品
我将向您展示如何在命令行中执行此操作,您可以将此代码应用于您的 View /模板:
from haystack.query import SearchQuerySet
# First we make a search to get some shops
# assuming you defined username field in search_indexes.py for Shop object
shop_list = SearchQuerySet().filter(username='something')
# or
shop_list = SearchQuerySet().filter(content='shop_username')
# Now (let's supose there is at least 1 shop) you can get the items like:
shop_list[0].object # This returns the shop object, so you can do
shop_list[0].object.item_set.all() # This returns all items belonging to this shop
在您的模板中,您可以尝试:
{{ object.object.item_set.all }} # This return a list of items belonging to shop
您可以生成这样的列表
<ul>
{% for item in object.object.item_set.all %}
<li>{{item}}</li>
{% endfor %}
</ul>
这可能会令人困惑,因为您将对象调用为变量,但您需要记住,SearchQuerySet 实例始终具有此对象
字段来访问原始Django 对象
我建议您检查Haystack - SearchQuerySet API
编辑
在您的模板中,您的结果位于变量 items
中,因此您可以执行以下操作:
# Assumming all the items are shops
{% for shop in items %}
{{shop.object.item_set}} # If the item is a SHop, this return all its items
{% endfor %}
要分离结果,因为我认为并非所有项目都是商店,您可以分离结果并将另一个变量中的商店发送到模板,例如:
def search(request):
form = ItemSearchForm(request.GET)
results = form.search()
return render(request, 'search/search.html', {
'items': results
'shops': results.model(Shop)
})
因此在您的模板中您可以执行以下操作:
{% for shop in shops %}
{{shop.object.item_set}}
{% endfor %}
编辑2
好吧,现在看来您的模板中有商品...您想要商店的所有商品吗?
- 哪家商店?与模板中的商品是同一家商店吗?
你说:
But how can I display all items that belong to a shop if someone includes a username or bio of that shop?
您正在使用 haystack 来搜索商品,但希望结果是商店?
您可以添加到您的item_text.txt文件中:
{{ object.tags}}
# If you add this and you look for shop username
# or bio it will return all items of the shop
{{ object.shop.username}}
{{ object.shop.bio}}
# Adding this 2 fields items are indexed with the related shop username and bio
或者您也可以开始为商店模型建立索引。
关于python - 从 Django Haystack 中的外键搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26730367/