gpt4 book ai didi

python - 如何使用具有自定义属性的 django-haystack 搜索面?

转载 作者:行者123 更新时间:2023-12-04 15:46:15 28 4
gpt4 key购买 nike

我有一个 Django-oscar 商店,我成功安装了 Solr 4.7.2 作为搜索引擎。它适用于预定义的属性,例如upc, title, product_class...

但过滤附加属性不起作用。

那是我的目录/models.py:

class Product(AbstractProduct):
video_url = models.URLField()
co2 = models.IntegerField()
is_public = models.BooleanField()
test = models.TextField()

在 search_indexes.py 中,我尝试添加如下内容:

co2 = indexes.IntegerField(model_attr="co2", null=True, indexed=False)

def prepare_co2(self, obj):
return self.apps.get_model().objects.filter(co2="2")
# return obj.co2 etc. here I tried a lot of code, but didnt work

我也尝试复制了这个功能的现成代码。

有人知道怎么做吗?当我过滤 catalogue.products.title 时,它​​可以正常工作,但不能用于 cataolgue.products.co2(我已经补充了自己)。

最佳答案

您不能从 prepare 函数中过滤对象,您只需要指定 haystack 如何访问对象字段。

from haystack import indexes
import oscar.apps.search.search_indexes as oscar_search_indexes


class ProductIndex(oscar_search_indexes.ProductIndex):
co2 = indexes.IntegerField(null=False, indexed=True)

def prepare_co2(self, obj):
return obj.co2

以上应该有效(一旦您在更新 Solr schema.xml 后重新索引了您的产品),如果没有,请使用您收到的错误或使用示例数据的意外查询行为更新您的问题。

关于python - 如何使用具有自定义属性的 django-haystack 搜索面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55658830/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com