title: Saving a model containing a FileField in Django
body: <p>I have the below model;</p>
  
  <pre><code>class AudioFile(models.Model):
      name = models.CharField(max_length=100, default='')
      audio_file = models.FileField()
      uploader = models.ForeignKey(User, default='')
  
      def __unicode__(self):
          return self.name    
  </code></pre>
  
  <p>The below form;</p>
  
  <pre><code>class AudioFileForm(forms.ModelForm):
      class Meta:
          model = AudioFile
          fields = ['name', 'audio_file']
  
      def clean_audio_file(self):
          audio = self.cleaned_data.get('audio_file',False)
          if audio:
              if audio._size &gt; 5*1024*1024:
                  raise ValidationError("File too large ( &gt; 5mb )")
              if os.path.splitext(audio.name)[1] != ".mp3":
                  raise ValidationError("We only support mp3!")
              return audio
          else:
              raise validationError("Couldn't read uploaded file")
  </code></pre>
  
  <p>As you can see there's some custom validations</p>
  
  <p>Then I have the below html with the <code>AudioFileForm</code> passed as <code>form</code>;</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;form method="post" action="{% url 'actual_upload_audio' %}"&gt;
      {% csrf_token %}
      {{ form }}
      &lt;input type="submit" value="Upload"/&gt;
    &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Now I know how to save the object had it not been containing a FileField. I would do something like this; </p>
  
  <pre><code>form = AudioFileForm(request.POST)
  if form.is_valid():
      name = form.cleaned_data['name']
      //Say it contained another field, foo
      foo = form.cleaned_data['foo'] 
      uploader = request.user //view requires login
      audio_file = Audio_File.objects.create(name=name,
                                             foo=foo,
                                             uploader=uploader)
      audio_file.save()
      return HttpResponse("Success!")
  </code></pre>
  
  <p>But there being a FileField in question I donno how to handle this. I read Django's documentation but don't really get it. A much easier elaborate explanation would be lovely.</p>
  
  <p>For a start I have <code>MEDIA_ROOT = '/home/afzalsh/works/openradio/audio_files/'</code> in my <code>settings.py</code> </p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks to @Rohan I now have <code>&lt;form method="post" action="{% url 'actual_upload_audio' %}" enctype="multipart/form-data"&gt;</code> instead of <code>&lt;form method="post" action="{% url 'actual_upload_audio' %}"&gt;</code></p>
  
  <p>Also have the below view thanks to @dzejdzej;</p>
  
  <pre><code>form = AudioFileForm(request.POST, request.FILES)
      if form.is_valid():
          form.cleaned_data['uploader'] = request.user
          form.save()
          return HttpResponseRedirect(reverse('home_audio',kwargs={'pk':audio_file.pk}))
      else:
          return HttpResponse("Form Invalid!")
  </code></pre>
  
  <p>But <code>Form Invalid!</code> :/</p>