I am using django Haystack
with ElasticSearch backend
. In my Model I have a DateTimeField
which is creating problems while rebuilding_indexes.
My Model is like this:
class MyModel(models.Model):
action = models.DateTimeField()
My Index Class is like:
Class MyModelIndex(indexes.SearchIndex, indexes.Indexable):
action_time = indexes.DateTimeField(null=True, model_attr="action")
The value i got in shell for the particular instance which is creating problem is
obj = MyModel.objects.get(id=1)
obj.action
Out[56]: datetime.datetime(2016, 6, 21, 14, 6, 37, 430691, tzinfo=<UTC>) # result or value of action field
And the error I am getting while creating indexes is
if not language_code_re.search(lang_code):
TypeError: expected string or buffer
I tried to return strftime from indexes from prepare field , but it also doesn't work
def prepare_action_time(self, obj):
return obj.action.strftime('%Y-%m-%dT%H:%M:%SZ') if obj.action else None
but it works if I return unicode representation of the datetime Value like
def prepare_action_time(self, obj):
return unicode(obj.action) if obj.action else None
or without using use_template=False
in my searchindex I am able to index the documents or the objects
But I am unable to get where the actual problem is. Help will be appreciated
To reproduce, start a new project using Django's project template. I believe that USE_L10N = True exposes the issue, though it might be a combination of settings that causes it.
Visit the same link Ivan given, and check your settings if USE_I10N
settings. Make it USE_L10N = False
from USE_L10N = True
, and your code will be working.
See https://code.djangoproject.com/ticket/24569 for more info.
change USE_L10N = True to USE_L10N = False,