我正在使用活塞为我正在编写的处理重复日历事件的应用程序编写 JSON api。
我的 API 适用于常规事件,当我尝试添加逻辑来处理重复事件时,我开始收到以下错误:
描述符 'date' 需要一个 'datetime.datetime' 对象,但收到了一个 'unicode'
这是我的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
是一个 IntegerField, actualDate
, startDate
, 并且endDate
都是 DateField。
我的 URLconf 接受 uid、年份和月份,所有这些都作为参数传递给 CalendarHandler.read 方法。