I have a Django model with many fields (roughly 24), and apparently my users would like to be able to create instances of this object using a spreadsheet upload instead of manually entering all the data into forms.
I've found that using python's built in CSV module should make this fairly simple, but I'm having a hard time working out how exactly to use it.
Let's start out with what I have in terms of code:
def upload_file(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_files(request.FILES['file'])
return HttpResponseRedirect('/workflow/')
else:
print form.errors
print request.FILES
return HttpResponseRedirect('/workflow/upload')
else:
form = UploadFileForm()
return render(request, 'fileform.html', {'formset': form})
This accepts a CSV file as an upload, and hands it off to handle_files
to deal with the parsing and the object creation. This is the method I'm having trouble with.
def handle_files(f):
reader = csv.DictReader(f, delimiter=' ', quotechar='|')
... #?
I've been trying to mimic the python docs (http://docs.python.org/2/library/csv.html), but DictReader is very poorly documented. Am I supplying csv.DictReader()
the appropriate arguments? If I have, how can I get the information out of reader
? I'm going to be supplying users with a template, so I can assume that each column of the CSV file has a predictable data. That is, I know that Column A will have data corresponding to field X, and column B corresponds to Y, etc. How do I go about parsing the data from the reader, and then creating an object with that data?
I'm guessing it will be something like:
for row in reader:
X=row[1]
Y=row[2]
#etc
my_object = MyObject(x=X, y=Y)
my_object.save()
Is this way off? Should I be using another type of CSV reader?
Thanks for any help, I know there are a lot of questions in this post.