title: Django formset is not valid- why not?
body: <p>I am trying to use a form to allow users to upload images to projects stored in a database in my Django project, however, I'm currently getting console output that's telling me that the <code>formset</code> I'm using is not valid...</p>
  
  <p>The view that I'm trying to use to upload the images to a project has been defined with:</p>
  
  <pre><code>def upload_budget_pdfs(request, project_id):
      project = Project.objects.get(id=project_id)
      print("Value of project in 'upload_budget_pdfs()': ", project)
  
      presentations = project.budget_versions.select_related('meeting').prefetch_related('budget_items', 'cci_items', 'presenters').filter(version_number__isnull=False).annotate(vn=F('version_number') * -1).order_by('presentation_date', 'created', '-vn')
      print("Value of presentations in 'upload_budget_pdfs()': ", presentations)
      drawing_formset = DrawingUploadFormset(prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))
      print("Value of drawing_formset in 'upload_budget_pdfs()': ", drawing_formset)
  
      if drawing_formset.is_valid():
          print 'Saving drawing_formset'
          print "Before", [b.id for b in project.budget_versions.all()]
          for drawing_form in drawing_formset:
              if drawing_form.instance.budget:
                  print 'Instance', drawing_form.instance.budget
                  drawing = drawing_form.save(commit=False)
                  drawing.budget = drawing_form.instance.budget
                  drawing.save()
              print drawing, [b.id for b in project.budget_versions.all()]
      else: print 'Drawing formset not valid.',drawing_formset.errors
  
      budget_formset = BudgetPresentationFormset(request.POST, request.FILES, instance=project, prefix="presentations")
  
      if budget_formset.is_valid() and budget_formset.has_changed():
          updated_budget_presentations = budget_formset.save()
      elif budget_formset.has_changed(): print 'Budget formset not valid.',budget_formset.errors
  
  
  
      return HttpResponseRedirect(reverse('projects:concept', args=[project_id]))
  </code></pre>
  
  <p>and the console output I'm getting when this view is called is:</p>
  
  <blockquote>
    <p>("Value of project in 'upload_budget_pdfs()': ", &lt; Project: Test 1 >)</p>
    
    <p>("Value of presentations in 'upload_budget_pdfs()': ", [&lt; Budget: Test 1: Version -1 >, &lt; Budget: Test 1: Version -1 >, &lt; Budget: Test 1: Version 0 >, &lt; Budget: Test 1: Version 0 >, , , , , , , , , , , , , , , , , '...(remaining elements truncated)...'])</p>
    
    <p>("Value of drawing_formset in 'upload_budget_pdfs()': ", &lt; django.forms.formsets.DrawingFormFormSet object at 0x10cee3790 >)</p>
    
    <p>Drawing formset not valid. []</p>
  </blockquote>
  
  <p>So it's clear that the <code>else</code> statement in my view is what is being run here, but I'm not sure why, given that the console clearly indicates that the variables all hold the correct data... </p>
  
  <p>What is wrong with the way I am initialising the <code>drawing_formset</code> variable here? Is the <code>queryset</code> I'm passing into <code>DrawingUploadFormset(...)</code> correct? What does <code>is_valid()</code> actually check?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I made the change in the accepted answer, so that my declaration of <code>drawing_formset</code> is now:</p>
  
  <pre><code>        drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))
  </code></pre>
  
  <p>But it seems I'm now getting an 'Internal Server Error' and <code>MultiValueDictKeyError</code> on the line:</p>
  
  <pre><code>for drawing_form in drawing_formset: #This line is what's causing the MultiValueDictKeyError
  </code></pre>
  
  <p>I don't understand why this is? Any suggestions?</p>