首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同的SearchQuerySet取决于页面

不同的SearchQuerySet取决于页面
EN

Stack Overflow用户
提问于 2014-12-28 19:49:38
回答 1查看 147关注 0票数 2

StackOverflow为我完成以下项目提供了很多帮助。然而,我陷入了一个问题--> Hay堆栈方面!,我读了几十个问题--答案,但没有一个能满足我的要求。

我正在使用Django建立一个电子商店,它将出售珠宝,小雕像,艺术物品等。我也使用django-mptt片段来组织我的分类。我想要的(只用于facet实现)类似于。因此,不同的方面取决于所选择的类别。我的结论是,为了达到这个目的,我必须根据用户点击的类别在MyFacetedSearchView's __init__中设置一个不同的__init__。我怎么做呢?我是不是弄错了?

我的档案:

代码语言:javascript
复制
#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。然后在我所有的页面(珠宝,陶瓷,雕像等)这些方面显示的是整个目录中的产品,而不是我所处的特定类别,即在珠宝页面中,它显示了所有与珠宝相关的产品,但在面(由创作者) div中,它显示了创作者A建造了珠宝,而创建者B没有(但B已经建造了,雕塑)。如何每次传递不同的SearchQuerySet来组织我的方面?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-01 20:12:14

好吧,5天后我想明白了。问题是,我希望根据页面的不同,出现一组不同的方面。

该解决方案位于extra_content类的FacetedSearchView方法中。在这个方法中,定义了( dict extra的)extra键。

我唯一需要做的就是在我的views中覆盖这个方法,并根据我所处的类别定义一个不同的facet_counts()

因此,代码流是:

  1. 首先,调用FacetedSearchViewFacetedSearchView方法并定义extra['facets'] = self.results.facet_counts()键值.
  2. 因为该方法在我的views中被重写,所以我还声明了extra['facets'] = something_else.facet_counts(),这是最后的值。
  3. 最后,我的.facet_counts()被呈现到模板上,我得到了我想要的东西。

也许这能帮到别人!

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27680027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档