【问题标题】:Different SearchQuerySet depending on the page不同的 SearchQuerySet 取决于页面
【发布时间】:2014-12-28 19:49:38
【问题描述】:

StackOverflow 对我完成以下项目有很大帮助。然而,我被困在一个点 --> Haystack Facets! 我已经阅读了几十个问题答案,但没有一个能满足我的情况。
我正在使用 Django 建立一个销售珠宝、小雕像、艺术品等的电子商店。我还使用 django-mptt sn-p 来组织我的类别。 我想要的(仅用于方面实现)类似于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 视图被初始化时,它会得到整个sqs。然后在我的所有页面(珠宝、陶瓷、雕像等)中,刻面显示整个目录中的产品,而不是我所在的特定类别,即在珠宝页面中,它显示所有与珠宝相关的产品,但在刻面( by creator) div,它显示了一个创造者 A 已经建造了珠宝和一个创造者 B 没有(但 B 已经建造了比如说雕像)。 如何每次传递不同的SearchQuerySet 来组织我的构面?

【问题讨论】:

    标签: django django-haystack faceted-search


    【解决方案1】:

    好的,5天后,我设法弄明白了。 问题是我希望根据页面显示一组不同的构面。
    解决方案位于 Haystack 的 FacetedSearchView 类的 extra_content 方法中。在此方法中,定义了 extra['facets'] 键(dict extra)。
    我唯一需要做的就是在我的 views 中覆盖此方法,并根据我所在的类别定义不同的 facet_counts()

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

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

    也许这对其他人有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-22
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多