django - 根据页面不同,SearchQuerySet 也不同

标签 django django-haystack faceted-search

StackOverflow 对我完成以下项目帮助很大。然而,我陷入了一个点 --> Haystack Facets! 我已经阅读了几十个问题答案,但没有一个能满足我的情况。
我正在使用 Django 构建一个电子商店,出售珠宝、小雕像、艺术品等。我还使用 django-mptt 片段来组织我的类别。 我想要的(仅用于方面实现)类似于 this 。因此,不同的方面取决于所选的类别。我得出的结论是,为了实现此目的,我必须根据用户单击的类别在 MyFacetedSearchView__init__ 中设置不同的 SearchQuerySet。我怎样才能做到这一点?我到底是记错了还是没记错?
我的文件:

#search_indexes.py

from haystack import indexes

from .models import Product

class ProductIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    creator = indexes.CharField(model_attr='creator', faceted=True)
    material = indexes.CharField(model_attr='material', null=True, faceted=True)
    category = indexes.MultiValueField(faceted=True)
    sizevenus = indexes.MultiValueField(null=True, faceted=True)

def get_model(self):
    return Product

def prepare_category(self, obj):
    """
    Prepares the categories for indexing.
    obj.categories.all() runs for each Product instance.
    Thus, if we have 10 products then this method will run 10 times (during rebuild_index or update_index command)
    creating each time a different list for the categories each product belongs to.
    """
    return [category.slug for category in obj.categories.all()]

def prepare_sizevenus(self, obj):
    """
    Same philosophy applies here for the size of the product. But this time we have explicitly told that
    we want the size for the VENUS products ONLY. The rest of the products of this e-shop have no sizes!
    """
    return [stock.size.name for stock in obj.productstock_set.filter(product__categories__slug='venus')]

def index_queryset(self, using=None):
    """
    This method defines the content of the QuerySet that will be indexed. It returns a list of Product instances
    where each one will be used for the prepare_***** methods above.
    """
    return self.get_model().objects.all()

#views.py

class ShowProductsByCategory(FacetedSearchView):
    def __init__(self):
    sqs = SearchQuerySet().facet('category').facet('creator').facet('sizevenus').facet('
    template = 'catalog/show_products_by.html'
    form_class = MyFacetedSearchForm
    super(ShowProductsByCategory, self).__init__(template=template, searchqueryset=sqs, form_class=form_class)


问题:
当 ShowProductsByCategory View 初始化时,它会获取整个 sqs。然后在我的所有页面(珠宝、陶瓷、雕像等)中,各个方面显示整个目录中的产品,而不是我所在的特定类别,即在珠宝页面中,它显示所有与珠宝相关的产品,但在各个方面( by Creator) div,它显示了创建者 A build 了珠宝,而创建者 B 没有 build 珠宝(但 B build 了雕像)。 我如何每次传递不同的 SearchQuerySet 来组织我的方面?

最佳答案

好的,5天后我已经弄清楚了。 问题是我希望根据页面显示一组不同的方面。
解决方案位于 extra_content 方法FacetedSearchView Haystack 类。在这个方法中 extra['facets']键(字典 extra 的)已定义。
我唯一要做的就是在我的 views 中覆盖这个方法。并定义不同的facet_counts()根据我所在的类别进行分组

所以,代码流程是这样的:

  1. 首先,extra_content方法FacetedSearchView被称为 extra['facets'] = self.results.facet_counts()键值已定义。
  2. 因为这个方法在我的 views 中被覆盖了,我也声明了extra['facets'] = something_else.facet_counts()这是最终值。
  3. 最后,我的.facet_counts()渲染到模板中,我就有了我想要的。

也许这会对其他人有所帮助!

关于django - 根据页面不同,SearchQuerySet 也不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27680027/

相关文章:

python - Ubuntu - Django Haystack 和 Solr 集成错误

solr - 使用 SOLR 对短语进行 Sitecore 分面

java - 使用 java 和 lucene 3.6 进行分面搜索的教程

mysql - 服务器上 django 的 syncdb 期间出错

elasticsearch - 根据特定领域中的特定值进行干草堆提升

django - 响应 header 和浏览器 cookie 中的 csrf token 值不同。 django 1.9 中的 csrf 验证失败

django - Django-haystack 使用 Solr 索引列表而不是原始数据

asp.net - 哪些搜索工具可用于 ASP.NET 站点?

python - 不允许 os.setsid 操作

django - redis.异常.LockError : Cannot release an unlocked lock after restarting celerybeat