我需要能够像这样对索引中的数据进行排序:
http://mywebapp.com/search/?q=something&sort_by=points-desc
换句话说,我需要在URL中传递“sort_by”的值。我试图搜索文档,但我找不到任何例子。
有人能给出一个如何实现这个目标的线索吗?
我正在使用Django Haystack 2.0和SearchView()
任何线索?
更新:干草堆搜索索引
class LinkViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
url = indexes.CharField(model_attr='url')
url_image = indexes.CharField(model_attr='url_image')
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return LinkView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
class TextViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
texto = indexes.CharField(model_attr='texto')
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return TextView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
class OffTopicLinkViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
url = indexes.CharField(model_attr='url')
url_image = indexes.CharField(model_attr='url_image')
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return OffTopicLinkView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
class OffTopicTextViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
texto = indexes.CharField(model_attr='texto')
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return OffTopicTextView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
class BusinessLinkViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
country = indexes.CharField(model_attr='country')
localidades = indexes.CharField(model_attr='localidades')
url = indexes.CharField(model_attr='url')
url_image = indexes.CharField(model_attr='url_image')
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return BusinessLinkView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
class BicycleAdViewIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
section = indexes.CharField(model_attr='section')
section_id = indexes.CharField(model_attr='section_id')
item_author_username = indexes.CharField(model_attr='item_author_username')
title = indexes.CharField(model_attr='title')
country = indexes.CharField(model_attr='country')
city = indexes.CharField(model_attr='city')
n_item_kind = indexes.CharField(model_attr='n_item_kind')
n_bicycle_ad_condition = indexes.CharField(model_attr='n_bicycle_ad_condition')
bicyclead_link_url = indexes.CharField(model_attr='bicyclead_link_url', null=True)
bicyclead_link_url_image = indexes.CharField(model_attr='bicyclead_link_url_image', null=True)
bicyclead_insite_image = indexes.CharField(model_attr='bicyclead_insite_image', null=True)
texto = indexes.CharField(model_attr='texto', null=True)
telephone = indexes.CharField(model_attr='telephone', null=True)
price = indexes.CharField(model_attr='price', null=True)
n_bicycle_ad_payment_type = indexes.CharField(model_attr='n_bicycle_ad_payment_type', null=True)
propriedades = indexes.CharField(model_attr='propriedades', null=True)
comments = indexes.CharField(model_attr='comments', null=True)
comments_count = indexes.CharField(model_attr='comments_count')
points = indexes.CharField(model_attr='points')
date_inserted = indexes.DateTimeField(model_attr='date_inserted')
def get_model(self):
return BicycleAdView
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date_inserted__lte=datetime.datetime.now())
问题应该是。如何通过“points”或“date_inserted”使用以下内容将GET参数传递给订单:
http://mywebapp.com/search/?q=something&sort_by=points-desc
答案 0 :(得分:1)
覆盖haystack的get_queryset方法并将排序字段添加到索引模型中,例如:
class LinkListView(ListView):
context_object_name = “link”
def get_queryset(self):
sort = self.request.GET.get(‘sort_by’)
if sort:
return Link.published.filter().order_by('{0}'.format(sort))
else:
return Link.published.filter().order_by('-date_inserted')