Django Read / Download CSV

I need to download and read CSV and then save to the database. I am new to below - this is what I have so far used using "django-adapters" (http://django-adaptors.readthedocs.org/en/latest/index.html). I know this is not much, just do it to find out more :)

I am struggling with a bit of my view (below). I do not know how to load and then read the file into the CodeCSvModel () function? Can anyone help explain? Thank you very much.:)

views.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)) 

forms.py

 class codeUploadForm(forms.Form): file = forms.FileField() place = forms.ModelChoiceField(queryset=Incentive.objects.all()) 

csvTool.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 
+4
source share
2 answers

Django only saves the downloaded file in memory if it is 2.5 MB or less, otherwise it is written to a temporary folder.

You do not see where you are actually writing a file from memory / temporary location to disk somewhere, for example.

 if request.method == 'POST': form = MyForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = request.FILES['html-file-attribute-name'] # Write the file to disk fout = open("path/to/save/file/to/%s" % uploaded_file.name, 'wb') for chunk in uploaded_file.chunks(): fout.write(chunk) fout.close() 

Chunking is the way to go if the file is very large, because Django will read every fragment of the file in memory before writing to disk. If you use read () instead, it will read the entire file into memory, so chunks are your best bet.

+5
source

So it worked, I was almost there:

  file = request.FILES['file'] CodeCSvModel.import_from_file(file) return render_to_response('import.html', context_instance=RequestContext(request)) 
+2
source

All Articles