I am using django-haystack for search. By default it is showing oldest objects first whereas i want to show latest on top. Can anyone guide me how can i do this?
My code sample is shown below:
search_indexes.py
class PostIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
created = indexes.DateTimeField(model_attr='created')
def get_model(self):
return Post
def index_queryset(self):
return self.get_model().objects.filter(created__lte=datetime.datetime.now())
Can anyone tell me the exact file where i’ll have to make changes and what changes? Do i have to make changes to query.py file in haystack? Query.py has
def order_by(self, *args):
"""Alters the order in which the results should appear."""
clone = self._clone()
for field in args:
clone.query.add_order_by(field)
return clone
How can i make changes to this to show the latest on the top?
You can set order_by in your haystack_urls.py (or whatever you called it) e.g