2014-12-28 2 views
2

StackOverflow는 다음 프로젝트 완료에 많은 도움이됩니다. 그러나, 나는 한 지점에 갇혀있다 ->건초 더미 패싯! 나는 수십 개의 질문을 읽었지만 대답은 내 경우를 만족시키지 못한다.
장고를 사용하여 보석, 작은 상, 예술적 물건 등을 판매 할 전자 상점을 구축하고 있습니다. 또한 장고 - mptt 스 니펫을 사용하여 카테고리를 구성합니다. 내가 원하는 (패싯 구현에만 해당)은 this과 같습니다. 따라서 선택한 카테고리에 따라 다른면이 있습니다. 나는 이것을 달성하기 위해 사용자가 클릭 한 카테고리에 따라 MyFacetedSearchView__init__에 다른 SearchQuerySet을 설정해야한다고 결론을 내렸다. 어떻게해야합니까? 내가 잘못 했는가 아닌가?
내 파일 :
페이지에 따라 다른 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를 가져 초기화
. 그럼 내 모든 페이지 (보석, 세라믹, 동상 등)에서 패싯은 전체 카탈로그에서 제품을 보여 주며, 내가 속한 특정 카테고리가 아니라 보석 페이지에서 모든 보석 관련 제품을 보여 주지만 패싯 (창조주에 의해) div, 보석을 지은 창조주 A와 그렇지 않은 창조주 B를 보여줍니다 (그러나 B는 동상을 세웠습니다). 내 패싯을 구성하기 위해 매번 다른 SearchQuerySet을 전달할 수 있습니까?

답변

2

5 일 후에 나는 그것을 알아낼 수 있었다. 문제는 페이지에 따라 다른 패싯 그룹을 표시하기를 원했던 것입니다.
해결책은 건초 더미의 FacetedSearchView 클래스의 extra_content 방법 내에 있습니다. 이 방법에서는 extra['facets'] 키 (dict extra)가 정의됩니다. ,

  1. 첫째 :
    내가해야 할 일을했을 유일한 것은 그래서, 코드의 흐름이 내 views에서이 메소드를 오버라이드 (override) 내가

    에있어 범주에 따라 다른 facet_counts() 그룹을 정의하는 것이 었습니다 extra_content 메서드가 FacetedSearchView이고 extra['facets'] = self.results.facet_counts() 키 - 값이 정의됩니다.
  2. 이 메서드는 내 views에서 재정의되었으므로 최종 값인 extra['facets'] = something_else.facet_counts()도 선언했습니다.
  3. 마지막으로 내 .facet_counts()이 템플릿으로 렌더링되고 내가 원하는 것을 가지고 있습니다.

어쩌면 다른 사람에게 도움이 될 것입니다!