Django Haystack - 如何通过时间戳订购?

时间:2011-11-05 17:50:13

标签: django-haystack

我是Django和Haystack的新手...我需要知道如何按日期/时间戳排序我的搜索结果,最近一次。

这是我的model.py:

class adsText(models.Model):
    hash          = models.TextField(primary_key=True)
    title         = models.TextField()
    description   = models.TextField()
    country       = models.TextField()
    zone          = models.TextField()
    location      = models.TextField()
    date_inserted = models.DateTimeField()
    class Meta:
        db_table = u'ads_text'
        managed = False

    def __unicode__(self):
        return self.title

我的search_indexes.py:

class adsTextIndex(indexes.SearchIndex):
    text            = indexes.CharField(document=True, use_template=True)
    title           = indexes.CharField(model_attr='title')
    description     = indexes.CharField(model_attr='description')
    zone            = indexes.CharField(model_attr='zone')
    location        = indexes.CharField(model_attr='location')
    date_inserted   = indexes.DateTimeField(model_attr='date_inserted')

    def index_queryset(self):
        """Used when the entire index for model is updated."""
        return adsText.objects.order_by('-date_inserted')
        #return adsText.objects.filter(date_inserted__lte=datetime.datetime.now())

site.register(adsText, adsTextIndex)

我的urls.py:

urlpatterns = patterns('',
    (r'^search/', include('haystack.urls')),
)

我已经测试过更改为search_indexes.py中的这行代码,但它没有按预期工作:

return adsText.objects.order_by('-date_inserted')

我已经读到了要走的路是在urls.py中使用这样的东西:

sqs = SearchQuerySet().order_by('-date_inserted')

我的疑问是如何将此代码添加到urls.py的“urlpatterns”中,有关如何实现此目的的任何线索?

最诚挚的问候,

1 个答案:

答案 0 :(得分:5)

看看这里:http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#views

具体来说,您可能希望您的urlconf看起来像这样:

from django.conf.urls.defaults import *
from haystack.query import SearchQuerySet
from haystack.views import SearchView

sqs = SearchQuerySet().order_by('-date_inserted')

urlpatterns = patterns('haystack.views',
    url(r'^search/$', SearchView(
        searchqueryset=sqs,
    )),
)