gpt4 book ai didi

django-haystack - Django Haystack - 如何按时间戳排序?

转载 作者:行者123 更新时间:2023-12-03 22:39:21 27 4
gpt4 key购买 nike

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

这是我的模型.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”,关于如何实现此目的的任何线索?

最好的问候,

最佳答案

看看这里: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,
)),
)

关于django-haystack - Django Haystack - 如何按时间戳排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8022072/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com