[英]Descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode'
I am using piston to write a JSON api for an application I am writing which handles recurring calendar events. 我正在使用活塞为我正在编写的处理重复日历事件的应用程序编写JSON API。
My API was working for regular events, when I attempted to add logic to handle the recurrence, I started getting the following error: 我的API适用于常规事件,当我尝试添加逻辑来处理重复时,我开始收到以下错误:
descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode'
描述符'date'需要'datetime.datetime'对象但收到'unicode'
Here is my handlers.py
: 这是我的
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. frequency
是IntegerField, actualDate
, startDate
和endDate
都是DateField。
My URLconf accepts a uid, year, and month, all of which are passed as parameters to the CalendarHandler.read method. 我的URLconf接受一个uid,year和month,所有这些都作为参数传递给CalendarHandler.read方法。
By using from datetime import datetime, timedelta
you have imported the datetime type from the datetime module. 通过使用
from datetime import datetime, timedelta
您已从datetime模块导入datetime类型。 Thus when you call datetime.date
you are calling a method on the datetime type. 因此,当您调用
datetime.date
您正在调用datetime类型的方法。
I think what you want is to use the date type from the datetime module: 我想你想要的是使用datetime模块中的日期类型:
from datetime import datetime, timedelta, date
. from datetime import datetime, timedelta, date
。 date(year, month, 1)
instead of datetime.date(year, month, 1)
. date(year, month, 1)
而不是datetime.date(year, month, 1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.