I have a SearchIndex
whose results I want to be sorted based on a DatetimeField
. However when I try to manage.py rebuild_index
, I get a ValueError
complaining about the datetime being a... datetime.
In case it matters, I use timezones and pytz, but for the sorting I want, timezones do not really matter, I just want the newest first
kind of thing.
The Index
I have removed some irrelevant fields.
class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
publish_date = indexes.DateTimeField(model_attr='publish_date')
The view/url pair
posts_sqs = SearchQuerySet().order_by('-publish_date')
urlpatterns += patterns(
'haystack.views',
url(r'^search/$', search_view_factory(
view_class=PostsSearchView,
template='pages/search.html',
searchqueryset=posts_sqs,
form_class=ThreeCharMinSearchForm), # a custom form
name='pages.search'),
)
The rebuild_index Exception
ValueError: datetime.datetime(2015, 1, 23, 16, 31, 28, tzinfo=<UTC>) is not unicode or sequence
I have tried to implement prepare_publish_date
methods that return strftime
representations ('%Y %m %d %H %M'
and '%d %m %Y %H %M'
) with both naive and aware datetimes, timetuples, "epoch times" with a CharField
instead of a DateTimeField
and I can't remember what else and all failed, except for the "epoch time" version, which was terribly slow though.
As a last note, I use Python 2.7.8, Django 1.6.10 and before I tried to do this sorting, the index was working nicely (even better that what was expected), so I am pretty sure the rest of the implementation is correct.
I understand that it is Whoosh that's expecting unicode, but I don't know what to do exactly. Any thoughts?