我需要上传并读取 CSV,然后保存到数据库。我是一个初学者,下面是我到目前为止使用'django-adaptors'(http://django-adaptors.readthedocs.org/en/latest/index.html)所取得的成就我知道它不是很多,但我'我这样做是为了了解更多信息:)
我在代码的视图中苦苦挣扎(如下)。我不知道如何上传然后将文件读入 CodeCSvModel() 函数?谁能帮忙解释一下?非常感谢。:)
视图.py
from django.template import RequestContext
from django.shortcuts import render_to_response
from web.forms import codeUploadForm
from web.csvTools import CodeCSvModel
def codeImport(request):
# If we had a POST then get the request post values.
if request.method == 'POST':
form = codeUploadForm(request.POST, request.FILES)
# handle_uploaded_file(request.FILES['file'])
====[HELP HERE]=====
#form = codeUploadForm(request.POST)
CodeCSvModel.import_from_file(form['file'])
else:
form = codeUploadForm()
context = {'form':form}
return render_to_response('import.html', context, context_instance=RequestContext(request))
表格.py
class codeUploadForm(forms.Form):
file = forms.FileField()
place = forms.ModelChoiceField(queryset=Incentive.objects.all())
csv工具.py
from datetime import datetime
from adaptor.fields import *
from adaptor.model import CsvModel, CsvDbModel, ImproperlyConfigured,\
CsvException, CsvDataException, TabularLayout, SkipRow,\
GroupedCsvModel, CsvFieldDataException
from web.models import *
class CodeCSvModel(CsvModel):
codeid = CharField()
remotecode = CharField()
active = BooleanField()
created = DateField()
modified = DateField()
incentiveid = CharField()
class Meta:
delimiter = ";"
dbModel = Code