追加到列表返回"list indices must be integers, not unicode"(django/python)



我正在尝试将对象附加到列表中,但一直收到错误list indices must be integers, not unicode.这对我来说没有任何意义,因为我没有以任何方式操纵列表索引......我只是创建一个新列表并向它附加()对象。

瞧:

def read(self, request, uid, month, year):
    qs = NewLesson.objects.filter(student__teacher = request.user).filter(endDate__gte=date(int(year), int(month), 1)).filter(startDate__lte=datetime.date(int(year), int(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.student = l.student
            lessonList.append(x)
        else:
            sd = next_date(l.startDate, l.frequency, datetime.date(int(year), int(month), 1))
            while (sd <= date(int(year), int(month), calendar.mdays[month])):
                x = EachLesson()
                x.lessonID = l.id
                x.actualDate = sd
                x.student = l.student
                lessonList.append(x)
                sd += datetime.timedelta(recurrence)
    return lessonList

为了这个例子,假设NewLesson 和 EachLesson 在模型中具有类似的结构。

提前感谢,

嗯,最大的提示是你唯一做过getitem调用的地方:mdays[month]

如果您必须在其他地方将month转换为int,则month很可能是导致错误的字符串calendar.mdays[month]

否则,这是您的回溯会识别的其他位置的调用。我的钱在mdays[month]尽管由于其他地方的int(month)

最新更新