我想将数据放入Excel工作表。
我写了以下代码:
data_dict ={}
data_dict =defaultdict(dict)
def try_to_int(arg):
try:
return int(arg)
except:
return arg
def main():
book4 = xlrd.open_workbook('./data/excel1.xlsx')
sheet4 = book4.sheet_by_index(0)
tag_list = sheet4.row_values(0)[1:]
for row_index in range(1, sheet4.nrows):
row = sheet4.row_values(row_index)[1:]
row = list(map(try_to_int, row))
value = dict(zip(tag_list, row))
data_dict[value['age']].update(value)
user2 = User.objects.filter(name=data_dict['name'])
print(user2)
if user2:
if data_dict['name'] == 'Tom':
user2.update(close_rate_under_300_ny = data_dict['300'],
close_rate_under_700_ny = data_dict['d700'],
close_rate_upper_700_ny = data_dictt['u700'])
elif data_dict['name'] == 'John':
user2.update(close_rate_under_300_eu = data_dict['300'],
close_rate_under_700_eu = data_dict['d700'],
close_rate_upper_700_eu = data_dict['u700'])
elif data_dict['name'] == 'Blear':
user2.update(close_rate_under_300_uk = data_dict['300'],
close_rate_under_700_uk = data_dict['d700'],
close_rate_upper_700_uk = data_dict['u700'])
else:
user2.update(close_rate_under_300_ch=data_dict['300'],
close_rate_under_700_ch = data_dict['d700'],
close_rate_upper_700_ch = data_dict['u700'])
main()
在print(user2)中,显示了许多<QuerySet []>
。所以我想也许用户2无法获得
此外,在if data_dict['name'] == 'Tom':
的if-else语句中,user2未更新。
我的代码有什么问题?顺便说一下,data_dict
正常。
答案 0 :(得分:0)
我认为您使用的是自定义用户模型。因此,如果您给出了User类及其方法的定义,那就更清楚了。
根据我的理解,我认为你需要使用
user2 = User.objects.get(name=data_dict['name'])
而不是:
user2 = User.objects.filter(name=data_dict['name'])
filter()返回一个Queryset,其中包含满足指定过滤器的对象。
因此,您需要使用“username”或“id”等用户的唯一属性才能获得单个用户。如果表中有多个具有相同名称的用户,则使用name将返回多个对象。
为了更新用户(因为您没有给出更新的定义),请尝试:
if user2:
if data_dict['name'] == 'Tom':
user2.close_rate_under_300_ny = data_dict['300']
user2.close_rate_under_700_ny = data_dict['d700']
user2.close_rate_upper_700_ny = data_dictt['u700']
user2.save()
elif data_dict['name'] == 'John':
user2.close_rate_under_300_eu = data_dict['300']
user2.close_rate_under_700_eu = data_dict['d700']
user2.close_rate_upper_700_eu = data_dict['u700']
user2.save()
elif data_dict['name'] == 'Blear':
user2.close_rate_under_300_uk = data_dict['300']
user2.close_rate_under_700_uk = data_dict['d700']
user2.close_rate_upper_700_uk = data_dict['u700']
user2.save()
else:
user2.close_rate_under_300_ch=data_dict['300']
user2.close_rate_under_700_ch = data_dict['d700']
user2.close_rate_upper_700_ch = data_dict['u700']
user2.save()
而不是
if user2:
if data_dict['name'] == 'Tom':
user2.update(close_rate_under_300_ny = data_dict['300'],
close_rate_under_700_ny = data_dict['d700'],
close_rate_upper_700_ny = data_dictt['u700'])
elif data_dict['name'] == 'John':
user2.update(close_rate_under_300_eu = data_dict['300'],
close_rate_under_700_eu = data_dict['d700'],
close_rate_upper_700_eu = data_dict['u700'])
elif data_dict['name'] == 'Blear':
user2.update(close_rate_under_300_uk = data_dict['300'],
close_rate_under_700_uk = data_dict['d700'],
close_rate_upper_700_uk = data_dict['u700'])
else:
user2.update(close_rate_under_300_ch=data_dict['300'],
close_rate_under_700_ch = data_dict['d700'],
close_rate_upper_700_ch = data_dict['u700'])