【发布时间】:2014-11-04 08:10:36
【问题描述】:
我有这两个模型:
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 %}
这是从标签中搜索并且工作正常。但是,如果有人包含该商店的用户名或简历,我如何显示属于该商店的所有商品?
【问题讨论】:
-
您在模板中使用变量
items做什么?在我的答案中检查我的编辑 -
我包含了模板代码。 @Liarez
-
我建议您检查我的edit2并更改您的items_text.txt文件并添加
{{item.shop.username}}和{{item.shop.bio}},因此如果您查找商店用户名,它将返回所有属于它的物品
标签: python django django-haystack