描述符'date'需要一个'datetime.datetime'对象,但是收到'unicode' [英] Descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode'

查看:165
本文介绍了描述符'date'需要一个'datetime.datetime'对象,但是收到'unicode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用活塞为我正在编写的应用程序编写一个JSON api来处理定期的日历事件。我的API正在为常规事件工作,当我尝试添加逻辑来处理复发,我开始收到以下错误:


描述符'date'需要一个'datetime.datetime'对象收到'unicode'


这是我的 handlers.py



  from piston.handler import BaseHandler 
来自lessons.models import NewEvent,EachEvent
import calendar
从datetime import datetime,timedelta

class CalendarHandler(BaseHandler):
allowed_methods =('GET',)
model = EachEvent
fields =('actualDate' ('manager',('firstName','lastName')))

def next_date(startDate,recurrence,rangeStart):
sd = startDate
while(sd& rangeStart):
print sd;
sd + = datetime.timedelta(recurrence)
return sd

def read(self,request,uid,month,year):
qs = NewEvent.objects .filter(manager__boss = request.user).filter(endDate__gte = datetime.date(year,month,1))。filter(startDate__lte = datetime.date(year,month,calendar.mdays [month]))
lessonList = []
qsList = list(qs)
在qsList中的l:
如果l.frequency == 0:
x = EachLesson()
x.lessonID = l.id
x.actualDate = l.startDate
x.manager = l.manager
lessonList.append(x)
else:
sd = next_date l.startDate,l.frequency,datetime.date(year,month,1))
while(sd< = datetime.date(year,month,calendar.mdays [month])):
x = EachLesson()
x.lessonID = l.id
x.actualDate = sd
x.manager = l.manager
lessonList .append(x)
sd + = datetime.timedelta(recurrence)

return lessonList

频率是一个IntegerField, actualDate startDate endDate 都是DateField。



我的URLconf接受uid,year和month,所有这些都作为参数传递给CalendarHandler.read方法。

解决方案

通过使用datetime import中的 datetime,timedelta 您已经从datetime模块导入了datetime类型。因此,当您调用 datetime.date 时,您将调用datetime类型的方法。



我想你想要的是要使用datetime模块中的日期类型:


  1. 将导入从datetime import datetime,timedelta更改为日期

  2. 致电日期(年,月,1)而不是 datetime.date(年,月,1)


I am using piston to write a JSON api for an application I am writing which handles recurring calendar events.

My API was working for regular events, when I attempted to add logic to handle the recurrence, I started getting the following error:

descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode'

Here is my handlers.py:

from piston.handler import BaseHandler
from lessons.models import NewEvent, EachEvent
import calendar
from datetime import datetime, timedelta

class CalendarHandler(BaseHandler):
allowed_methods = ('GET',)
model = EachEvent
fields = ('actualDate', ('manager', ('firstName', 'lastName')))

def next_date(startDate, recurrence, rangeStart):
    sd = startDate
    while (sd < rangeStart):
        print sd;
        sd += datetime.timedelta(recurrence)
    return sd

def read(self, request, uid, month, year):
    qs = NewEvent.objects.filter(manager__boss = request.user).filter(endDate__gte=datetime.date(year, month, 1)).filter(startDate__lte=datetime.date(year, month, calendar.mdays[month]))
    lessonList = []
    qsList = list(qs)
    for l in qsList:
        if l.frequency == 0:
            x = EachLesson()
            x.lessonID = l.id
            x.actualDate = l.startDate
            x.manager = l.manager
            lessonList.append(x)
        else:
            sd = next_date(l.startDate, l.frequency, datetime.date(year, month, 1))
            while (sd <= datetime.date(year, month, calendar.mdays[month])):
                x = EachLesson()
                x.lessonID = l.id
                x.actualDate = sd
                x.manager = l.manager
                lessonList.append(x)
                sd += datetime.timedelta(recurrence)

    return lessonList

frequency is an IntegerField, actualDate, startDate, and endDate are all DateField.

My URLconf accepts a uid, year, and month, all of which are passed as parameters to the CalendarHandler.read method.

解决方案

By using from datetime import datetime, timedelta you have imported the datetime type from the datetime module. Thus when you call datetime.date you are calling a method on the datetime type.

I think what you want is to use the date type from the datetime module:

  1. Change your import to from datetime import datetime, timedelta, date.
  2. Call date(year, month, 1) instead of datetime.date(year, month, 1).

这篇关于描述符'date'需要一个'datetime.datetime'对象,但是收到'unicode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆