Skip to main content
 首页 » 编程设计

python之从 Django Haystack 中的外键搜索

2024年10月25日29tintown

我有这两个模型:

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 实例总是有这个object 字段来访问原始数据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 

或者您也可以开始索引 Shop 模型。