I'm using Django 1.11 with Postgresql 9.6 and JQuery DataTables (using django-datatables to provide AJax datasource from Djang model.
Model example:
class MyModel(models.Model):
start = models.DateTimeField()
end = = models.DateTimeField()
Postgresql stores the datetimes in UTC with timezone info, which is fine.
I can override the DataTables render column to render the datetime correctly in the DataTables view:
if column == "start":
return timezone.localtime(row.start).strftime(STRFTIME_DATETIME_FORMAT)
The problems when trying to provide the search filter query for partial dates. If I add an annotation to add date_str to search on:
def filter_queryset(self, qs):
search = self.request.GET.get(u'search[value]', None)
if search:
sql_datetime_format = getattr(settings, "SQL_DATETIME_FORMAT", "DD/MM/YYYY HH24:MI")
qs = qs.annotate(
start_str=Func(F('start'), Value(sql_datetime_format), function='to_char'),
end_str=Func(F('end'), Value(sql_datetime_format), function='to_char'),
)
q_objects = Q()
q_objects |= Q(start_str__icontains=search)
q_objects |= Q(end_str__icontains=search)
qs = qs.filter(q_objects).distinct()
return qs
The start_str and end_str are converted to strings as UTC datetimes not local datetimes.
So I UK date in summer displays correctly as 01/06/2017 00:00, but to search for it you have to enter: 31/05/2017 23:00
I can't seem to find away to get start_str and end_str to local time instead of UTC.