Django 不同的SearchQuerySet取决于页面

Django 不同的SearchQuerySet取决于页面,django,django-haystack,faceted-search,Django,Django Haystack,Faceted Search,StackOverflow为我完成以下项目提供了很多帮助。然而,我被困在一个点上-->干草堆方面我已经阅读了几十个问题和答案,但没有一个能满足我的要求。 我正在使用Django建立一个电子商店,出售珠宝、小雕像、艺术品等。我还使用Django mptt片段来组织我的分类。 我想要的(仅用于方面实现)是这样的。因此,不同的方面取决于所选的类别。我的结论是,为了实现这一点,我必须在MyFacetedSearchView的\uuuu init\uuuu中设置不同的SearchQuerySet,具体取决

StackOverflow为我完成以下项目提供了很多帮助。然而,我被困在一个点上-->干草堆方面我已经阅读了几十个问题和答案,但没有一个能满足我的要求。
我正在使用Django建立一个电子商店,出售珠宝、小雕像、艺术品等。我还使用Django mptt片段来组织我的分类。 我想要的(仅用于方面实现)是这样的。因此,不同的方面取决于所选的类别。我的结论是,为了实现这一点,我必须在
MyFacetedSearchView
\uuuu init\uuuu
中设置不同的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。然后,在我的所有页面(珠宝、陶瓷、雕像等)中,刻面显示了整个目录中的产品,而不是我所处的特定类别,即在珠宝页面中,它显示了所有与珠宝相关的产品,但在刻面(按创建者)部分,它显示了一个已制作珠宝的创建者a和一个未制作(但B已制作,例如,雕像)的创建者B。
如何每次通过不同的
SearchQuerySet
来组织我的facet?

好的,5天之后我终于找到了答案。 问题是,我希望根据页面的不同显示不同的方面组。
解决方案在于Haystack的
FacetedSearchView
类的方法中。在这个方法中,定义了
extra['facets']
键(dict
extra

我要做的唯一一件事就是在我的
视图中重写此方法,并根据我所属的类别定义一个不同的
facet\u counts()


因此,代码流如下所示:

  • 首先,调用
    FacetedSearchView
    extra\u content
    方法,并定义
    extra['facets']=self.results.facet\u counts()
    键值
  • 由于此方法在我的
    视图中被重写
    ,因此我还声明了
    extra['facets']=something.facet\u counts()
    ,这是最终值
  • 最后,我的
    .facet\u counts()
    被呈现到模板中,我得到了我想要的。
  • 也许这会帮助别人