我正在将csv文件上传到django模型,这可以正常使用下面的代码
def upload_csv(request):
if request.method == 'POST' and request.FILES['csv_file']:
myfile = request.FILES['csv_file']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
data = csv.reader(fs.open(filename, mode='r'))
for row in data:
if row[0] != 'FP_Item':
post = FP()
post.FP_Item = row[0]
post.save()
messages.success(request, "FP uploaded to Database")
queryset_list = FP.objects.all()
context = {'object_list': queryset_list}
return render(request, 'index.html', context)
我需要的是防止重复数据库条目。当我看到重复的字段时,我需要继续而不用post.save()保存,所以我按照下面的方式修改了我的代码,但仍然保存了它的重复。代码中也没有错误。我做错了什么?
for row in data:
queryset_list = FP.objects.all()
if row[0] != 'FP_Item':
if row[0] == queryset_list.filter(FP_Item__contains=row[0]):
continue
post = FP()
post.FP_Item = row[0]
post.save()
答案 0 :(得分:0)
::min()
永远不会等于row[0]
,它仍然是一个查询集。你可以这样做:
queryset_list.filter