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

时间:2019-04-12 20:13:57

标签: python django solr django-haystack django-oscar

我有一家Django-oscar商店,并且成功安装了Solr 4.7.2作为搜索引擎。它非常适合预定义的属性,例如upc,标题,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(我自己补充过)配合使用。

2 个答案:

答案 0 :(得分:0)

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

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之后,一旦您重新索引了产品),否则,请使用收到的错误或带有示例数据的意外查询行为来更新问题。

答案 1 :(得分:0)

我的 django-oscar 版本有问题。我更新了它,现在我可以成功地重新索引 schema.xml。

相关问题