追加到list返回“列表索引必须是整数,而不是unicode”(Django/python)

2024-09-24 02:16:23 发布

您现在位置: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在模型中具有相似的结构。在

提前谢谢你


Tags: 对象列表datetimedaterequestsdfilteryear
1条回答
网友
1楼 · 发布于 2024-09-24 02:16:23

好吧,最大的提示是唯一的地方,您已经完成了getitem调用:mdays[month]

{{{cd2}最有可能的是{cd2}中的

否则,你的回溯会在其他地方找到一个电话。我的钱放在mdays[month]上,不过是因为int(month)在别处。在

相关问题 更多 >