【发布时间】: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