title: Parse CSV with Django and csv.DictReader
body: <p>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. </p>
  
  <p>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. </p>
  
  <p>Let's start out with what I have in terms of code: </p>
  
  <pre><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})
  </code></pre>
  
  <p>This accepts a CSV file as an upload, and hands it off to <code>handle_files</code> to deal with the parsing and the object creation. This is the method I'm having trouble with.</p>
  
  <pre><code>def handle_files(f):
      reader = csv.DictReader(f, delimiter=' ', quotechar='|')
      ... #?
  </code></pre>
  
  <p>I've been trying to mimic the python docs (<a href="http://docs.python.org/2/library/csv.html">http://docs.python.org/2/library/csv.html</a>), but DictReader is very poorly documented. Am I supplying <code>csv.DictReader()</code> the appropriate arguments? If I have, how can I get the information out of <code>reader</code>? 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? </p>
  
  <p>I'm guessing it will be something like: </p>
  
  <pre><code>for row in reader:
      X=row[1]
      Y=row[2]
      #etc
      my_object = MyObject(x=X, y=Y)
      my_object.save()
  </code></pre>
  
  <p>Is this way off? Should I be using another type of CSV reader? </p>
  
  <p>Thanks for any help, I know there are a lot of questions in this post. </p>