问题描述
我正在使用django oscar在电子商务网站上工作。列出产品,我必须应用过滤器(例如年,价格等)。我们可以使用奥斯卡面来进行此过滤功能吗?
我已尝试根据,但不知道如何使其正常工作和渲染。
OSCAR_SEARCH_FACETS = {
'fields':OrderedDict([
('product_class',{'name':_('Type'),'field':'product_class '}),
('rating',{'name':_('Rating'),'field':'rating'}),
]),
'queries' OrderedDict([
('price_range',
{
'name':_('Price range)),
'field':'price',
'查询':[
#这是一个列表(名称,查询)元组,名称将
#显示在前端
(_('0到20') ,你''(0-40)'),
(_('20至40'), ,你'[40到60]'),
(_('60+'),你'[60 TO *]'),
]
}),
])
}
甚至可以使用它来过滤产品,还是还有其他一些方法?
简单答案
可以使用奥斯卡过滤基于各种搜索方面的产品。您将不得不使用除了haystack的简单后端之外的搜索后端。
您在问题中的代码中提到的方面是默认可用的。奥斯卡提供更多的字段和查询,可以显示为搜索过滤器。如果您希望您的应用程序自定义的字段,并且默认情况下没有由oscar索引(例如自定义产品属性),请查看详细的答案。
详细答案
在最新版本的oscar上,您可以添加额外的过滤器,如年份,品牌,场景,默认情况下不会出现在oscar中。
搜索后端
首先,使用除Haystack简单后端之外的搜索后端。 Django Oscar正式支持Apache Solr()。如果您使用最新版本的Solr,您可能需要对 schema.xml 和 solrconfig.xml
$ b $进行一些更改b
索引字段
某些字段(例如产品的价格或 product_class em>由Django Oscar自动索引。如果要索引其他字段(例如产品属性),则必须编辑奥斯卡搜索应用程序的 search_indexes.py 以指定要索引的新字段。推荐的方法来克隆奥斯卡的搜索应用程序来指定您自己的 search_indexes.py 。
添加新的字段后,不要忘记重新编号索引。
指定搜索方面
可以使用 settings.py 中的OSCAR_SEARCH_FACETS设置来指定在前端显示哪些方面。指定OSCAR_SEARCH_FACETS的字段和查询,重新启动服务器,您将看到前端的新构面。
I am working on an ecommerce website using django oscar.Products are listed and I have to apply filters (e.g. year, price, etc.). Can we use oscar facet for this filtering functionality?
I have tried adding following as per oscar documentation, but don't know how to make it work and render on my front end.
OSCAR_SEARCH_FACETS = {
'fields': OrderedDict([
('product_class', {'name': _('Type'), 'field': 'product_class'}),
('rating', {'name': _('Rating'), 'field': 'rating'}),
]),
'queries': OrderedDict([
('price_range',
{
'name': _('Price range'),
'field': 'price',
'queries': [
# This is a list of (name, query) tuples where the name will
# be displayed on the front-end.
(_('0 to 20'), u'[0 TO 20]'),
(_('20 to 40'), u'[20 TO 40]'),
(_('40 to 60'), u'[40 TO 60]'),
(_('60+'), u'[60 TO *]'),
]
}),
])
}
Is it even possible to use this for filtering of products, or is there some other way?
Simple Answer
Yes, you can use oscar to filter products based on various search facets. You will have to use a search backend other than haystack's simple backend.
The facets that you have mentioned in the code in your question is what is available by default. Oscar offers more fields and queries that can be displayed as search filters. If you want fields that are custom to your application and not indexed by oscar by default(ex. custom product attributes), check out the detailed answer.
Detailed Answer
On the latest version of oscar, this is how you can add additional filters such as year, brand, occasion which are not present in oscar by default.
Search Backend
First, use a search backend other than Haystack's simple backend. Django oscar officially supports Apache Solr(Documentation here). If you are using the latest version of Solr, you might have to make some changes to schema.xml and solrconfig.xml
Indexing Fields
Certain fields such as price of the product or product_class are automatically indexed by Django Oscar. If you want to index other fields such as product attributes, you will have to edit oscar search app's search_indexes.py to specify the new fields to index. The recommended way to fork oscar's search app to specify your own search_indexes.py.Don't forget to reindex the products after you had added new fields to be indexed.
Specify Search FacetsThe OSCAR_SEARCH_FACETS setting in settings.py can be used to specify what facets to display on the front end. Specify the fields and queries for OSCAR_SEARCH_FACETS, restart your server and you will see the new facets on the front end.
这篇关于OSCAR_SEARCH_FACETS用于过滤产品列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!