DateTimeField收到一个天真的日期时间,没有解释

时间:2015-12-14 15:45:09

标签: django python-2.7 datetime

我对这个警告发疯了。

我正在研究Django,这个小问题给我带来了麻烦。这个观点之前有效,但突然停止了工作,一个天真的日期时间警告出现了。在命令行中,它会反复迭代,如下一个图像所示,并且内容未显示: Command Line Output for Warning

意见如下:

@login_required(login_url='/accounts/login/')
def loggedin(request):
    data = []
    data2 = []
    data3 = []
    dicdata2 = {}
    dicdata3 = {}
    datainterior = []
    today = timezone.localtime(timezone.now()+timedelta(hours=1)).date()
    tomorrow = today + timedelta(1)
    semana= today - timedelta(7)
    today = today - timedelta(1)
    semana_start = datetime.combine(today, time())
    today_start = datetime.combine(today, time())
    today_end = datetime.combine(tomorrow, time())
    for modulo in Repository.objects.values("des_especialidade").distinct():
        dic = {}
        mod = str(modulo['des_especialidade'])
        dic["label"] = str(mod)
        dic["value"] = Repository.objects.filter(des_especialidade__iexact=mod).count()
        data.append(dic)
    for modulo in Repository.objects.values("modulo").distinct():
        dic = {}
        mod = str(modulo['modulo'])
        dic["label"] = str(mod)
        dic["value"] = Repository.objects.filter(modulo__iexact=mod, dt_diag__gte=semana_start).count()
        datainterior.append(dic)
        # print mod, Repository.objects.filter(modulo__iexact=mod).count()
        # data[mod] = Repository.objects.filter(modulo__iexact=mod).count()
    dicdata2['values'] = datainterior
    dicdata2['key'] = "Cumulative Return"
    dicdata3['values'] = data
    dicdata3['color'] = "#d67777"
    dicdata3['key'] = "Diagnosticos Identificados"
    data3.append(dicdata3)
    data2.append(dicdata2)


#-------sunburst
databurst = []
dictburst = {}
dictburst['name'] = "CHP"
childrenmodulo = []
for modulo in Repository.objects.values("modulo").distinct():
    childrenmodulodic = {}
    mod = str(modulo['modulo'])
    childrenmodulodic['name'] = mod
    childrenesp = []
    for especialidade in Repository.objects.filter(modulo__iexact=mod).values("des_especialidade").distinct():
        childrenespdic = {}
        esp = str(especialidade['des_especialidade'])
        childrenespdic['name'] = esp
        childrencode = []
        for code in Repository.objects.filter(modulo__iexact=mod,des_especialidade__iexact=esp).values("cod_diagnosis").distinct():
            childrencodedic = {}
            codee= str(code['cod_diagnosis'])
            childrencodedic['name'] = 'ICD9 - '+codee
            childrencodedic['size'] = Repository.objects.filter(modulo__iexact=mod,des_especialidade__iexact=esp,cod_diagnosis__iexact=codee).count()
            childrencode.append(childrencodedic)
        childrenespdic['children'] = childrencode



        #childrenespdic['size'] = Repository.objects.filter(des_especialidade__iexact=esp).count()
        childrenesp.append(childrenespdic)
    childrenmodulodic['children'] = childrenesp
    childrenmodulo.append(childrenmodulodic)
dictburst['children'] = childrenmodulo
databurst.append(dictburst)
# print databurst



# --------stacked area chart
datastack = []
for modulo in Repository.objects.values("modulo").distinct():
    datastackdic = {}
    mod = str(modulo['modulo'])
    datastackdic['key'] = mod
    monthsarray = []
    year = timezone.localtime(timezone.now()+timedelta(hours=1)).year
    month = timezone.localtime(timezone.now()+timedelta(hours=1)).month
    last = timezone.localtime(timezone.now()+timedelta(hours=1)) - relativedelta(years=1)
    lastyear = int(last.year)
    lastmonth = int(last.month)
    #i = 1
    while lastmonth <= int(month) or lastyear<int(year):
        date = str(lastmonth) + '/' + str(lastyear)
        if (lastmonth < 12):
            datef = str(lastmonth + 1) + '/' + str(lastyear)
        else:
            lastmonth = 01
            lastyear = int(lastyear)+1
            datef = str(lastmonth)+'/'+ str(lastyear)
            lastmonth = 0
        datainicial = datetime.strptime(date, '%m/%Y')
        datafinal = datetime.strptime(datef, '%m/%Y')
        #print "lastmonth",lastmonth,"lastyear", lastyear
        #print "datainicial:",datainicial,"datafinal: ",datafinal
        filtro = Repository.objects.filter(modulo__iexact=mod)
        count = filtro.filter(dt_diag__gte=datainicial, dt_diag__lt=datafinal).count()
        conv = datetime.strptime(date, '%m/%Y')
        ms = datetime_to_ms_str(conv)
        monthsarray.append([ms, count])
        #i += 1
        lastmonth += 1
    datastackdic['values'] = monthsarray
    datastack.append(datastackdic)
    #print datastack


if request.user.last_login is not None:
    #print(request.user.last_login)
    contador_novas = Repository.objects.filter(dt_diag__lte=today_end, dt_diag__gte=today_start).count()
return render_to_response('loggedin.html',
                          {'user': request.user.username, 'contador': contador_novas, 'data': data, 'data2': data2,
                           'data3': data3,
                           'databurst': databurst, 'datastack':datastack})

1 个答案:

答案 0 :(得分:1)

我认为你的陈述如下:

datainicial = datetime.strptime(date, '%m/%Y')

正在创建没有时区支持的本机python时间。当您对这些变量应用查询或赋值给django日期字段时,它会给您警告。你必须让所有时区都知道,以便抑制警告。

一个简单的解决方法是:

from django.utils import timezone

datainicial = datetime.strptime(date, '%m/%Y')
datainicial = timezone.make_aware(datainicial, timezone.utc)
相关问题