Initiate pricing strategy inside the search index class in order to make it easier to override.
Despite you cannot customize `ProductIndex` class, you may need inherit original search index class and override necessary functionality or add new fields.
Afterwards, do not forget to exclude original search index in the Haystack settings.
@@ -5,7 +5,6 @@ from oscar.core.loading import get_class, get_model
# Load default strategy (without a user/request)
is_solr_supported = get_class('search.features', 'is_solr_supported')
Selector = get_class('partner.strategy', 'Selector')
-strategy = Selector().strategy()
class ProductIndex(indexes.SearchIndex, indexes.Indexable):
@@ -31,6 +30,8 @@ class ProductIndex(indexes.SearchIndex, indexes.Indexable):
date_created = indexes.DateTimeField(model_attr='date_created')
date_updated = indexes.DateTimeField(model_attr='date_updated')
+ _strategy = None
+
def get_model(self):
return get_model('catalogue', 'Product')
@@ -57,7 +58,13 @@ class ProductIndex(indexes.SearchIndex, indexes.Indexable):
# most common case is for customers to see the same prices and stock levels
# and so we implement that case here.
+ def get_strategy(self):
+ if not self._strategy:
+ self._strategy = Selector().strategy()
+ return self._strategy
+
def prepare_price(self, obj):
+ strategy = self.get_strategy()
result = None
if obj.is_parent:
result = strategy.fetch_for_parent(obj)
@@ -70,6 +77,7 @@ class ProductIndex(indexes.SearchIndex, indexes.Indexable):
return result.price.excl_tax
def prepare_num_in_stock(self, obj):
+ strategy = self.get_strategy()
if obj.is_parent:
# Don't return a stock level for parent products
return None
