【发布时间】:2020-06-22 20:50:27
【问题描述】:
我有一个数组数组。每个嵌套数组都包含有关学生的信息。然后我将对其进行迭代并将每个数组保存到一个学生对象中并将其持久化到我的数据库中。
students = [
["James", "Smith", 4, 10],
# more students here
]
for s in students:
student = Student()
student.first_name = s[0],
student.last_name = s[1],
student.classroom = s[2],
student.grade1 = s[3],
student.save()
Student 类中的字段classroom 定义为FloatField。
我收到以下错误:
TypeError: 字段 'classroom' 需要一个数字,但得到 (4,)。
这可能是什么原因?
编辑 1:错字
【问题讨论】:
-
问题是所有变量行的尾随逗号。您实际上是在将值设置为
4, -
使用 float(s[2]) 不起作用。 @match 如何更改线路以使其正常工作?
-
去掉逗号。
标签: python python-3.x django django-models