I’m new to Django and Haystack… I need to know how can I order my search results by date/timestamp, most recent first.
Here is my 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
My 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)
My urls.py:
urlpatterns = patterns('',
(r'^search/', include('haystack.urls')),
)
I have tested to change to this line of code in search_indexes.py but it not working as expected:
return adsText.objects.order_by('-date_inserted')
I have read that the way to go is to use something like this in the urls.py:
sqs = SearchQuerySet().order_by('-date_inserted')
My doubt is how can I add this code to the “urlpatterns” of urls.py, any clue on how to achieve this?
Best Regards,
Have a look here: http://django-haystack.readthedocs.org/en/latest/views_and_forms.html#views
Specifically, you probably want your urlconf to look like this: