2017-10-17 81 views
0

我使用Django 1.11 PostgreSQL的9.6和JQuery數據表(使用Django-數據表,以提供從Djang模型AJAX數據源Django的PostgreSQL及註釋日期時間字段到的localtime

模型例子:

class MyModel(models.Model): 
    start = models.DateTimeField() 
    end = = models.DateTimeField() 

。PostgreSQL的存儲日期時間與時區的信息,這是很好的UTC

我可以重寫數據表列渲染正確的數據表呈現的日期時間查看:

if column == "start": 
    return timezone.localtime(row.start).strftime(STRFTIME_DATETIME_FORMAT) 

嘗試爲部分日期提供搜索過濾器查詢時出現的問題。如果我添加註釋添加到DATE_STR上搜索:

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 

的start_str和end_str爲UTC日期時間不是本地的日期時間轉換爲字符串。

所以我英國迄今在夏季正確顯示爲2017年1月6日00:00,但尋找它,你必須輸入:31/05/2017 23:00

我似乎無法到找到將start_str和end_str分配給本地時間而不是UTC。

回答

0

爲了對付我結束了寫我自己的Django表達對PostgreSQL的日期和時間:

from django.db.models import Func 

def ToChar(expression, output): 
    ''' 
    Custom query to convert timestamp to string. 

    Example usage 
    queryset.annotate(
     created_date_str=ToChar('created_date', 'DD/MM/YYYY HH25:MI') 
     ) 
    ''' 

    class ToCharWithoutTZ(Func): 

     function = "TO_CHAR" 
     template = '%(function)s(%(expressions)s, \'{output}\')'.format(output=output) 

    return ToCharWithoutTZ(expression) 

def ToCharTZ(expression, timezone, output): 
    ''' 
    Custom query to convert timestamp to string in requested time zone. 

    Example usage 
    queryset.annotate(
     created_date_str=ToCharTZ('created_date', 'GB', 'DD/MM/YYYY HH25:MI') 
     ) 
    ''' 

    class ToCharWithTZ(Func): 

     function = "TO_CHAR" 
     template = '%(function)s(%(expressions)s AT TIME ZONE \'{timezone}\', \'{output}\')'.format(timezone=timezone, output=output) 

    return ToCharWithTZ(expression) 

用法示例:

from myapp.models.functions import ToCharTZ 

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=ToCharTZ('start', 'GB', sql_datetime_format), 
      end_str=ToCharTZ('end', 'GB', sql_datetime_format), 
     ) 
    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 
0

一些幫助從#django IRC頻道我已經在Django的PG-utils包使用DateTZ解決了這個:

要安裝:

pip install django-pg-utils 

更新代碼片段:

from pg_utils.utils import DateTZ 

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(DateTZ(F('start'), 'GB'), Value(sql_datetime_format), function='to_char'), 
      end_str=Func(DateTZ(F('end'), 'GB'), 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 

希望這會對其他人有用。

+0

這並沒有拉回時間信息,只有DateTZ與日期部分一起工作,請參閱下一個答案。 – Steve

相關問題