[英]invalid literal for int() with base10 in django query
I am trying to import an excel file into my database. 我正在尝试将excel文件导入我的数据库。 The file contains an id column which is later saved to Foreignkey filed in django model.
该文件包含一个id列,该列随后将保存到以Django模型归档的Foreignkey中。
st_id = row[10]
st_data = Student.objects.get(id=st_id)
Now when i save this data with .save() method, it saves the data but returns an unbound error 'st_data' referenced before assignment. 现在,当我使用.save()方法保存此数据时,它将保存数据,但返回分配前引用的未绑定错误'st_data'。 The traceback is
invalid literal for int() with base 10
. invalid literal for int() with base 10
该追溯是invalid literal for int() with base 10
。
Whats actually happening? 实际发生了什么? It saves the correct data all right, but throws the invalid literal for int().
它会保存正确的数据,但是会为int()抛出无效的文字。
EDIT: The overall concept is as follows: 编辑:总体概念如下:
try:
st_id = row[10]
print type(st_id) #this line returns both type float and str
st_data = Student.objects.get(id=st_id)
except Exception as e:
print e #here it throws an exception `invalid literal for int()`
st_dict = {
'student': st_data,
}
obj = Section(**st_dict)
obj.save() #the correct data is saved to the database.
Probably, your row doesn't contains int's. 您的行可能不包含int。
To fix it, just try: 要解决此问题,请尝试:
st_data = Student.objects.get(id=int(st_id))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.