title: Uploading a file with Django REST framework ListCreateAPIView
body: <p>Using Django REST framework's generic <code>ListCreateAPIView</code>, I've created an end point that I think should be able to upload photos via POST requests. I am modeling my code on <a href="http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html" rel="nofollow noreferrer">this tutorial</a>.</p>
  
  <p>So far I have attempted to POST a file to this endpoint using both Android and curl, and observed the same behavior: a new record is created, but the file is not attached. Because the file is a required field, the server then returns a 500 error.</p>
  
  <p><a href="https://stackoverflow.com/questions/20303252/django-rest-framework-imagefield?rq=1">This question</a> looks similar, but he's not using REST framework's generic views and I'm not sure why... I'd like to take advantage of them where applicable.</p>
  
  <p>Here is my Django view:</p>
  
  <pre class="lang-python prettyprint-override"><code>class PhotoList(generics.ListCreateAPIView):
      model = Photo
      serializer_class = PhotoSerializer
      permission_classes = [
          permissions.AllowAny
      ]
  </code></pre>
  
  <p>...my model:</p>
  
  <pre><code>def get_unique_image_file_path(instance=None, filename='dummy.jpg'):
      """
      function to determine where to save images.  assigns a uuid (random string) to each and places it
      in the images subdirectory below media.  by default, we assume the file is a .jpg
      """
      ext = filename.split('.')[-1]
      filename = "%s.%s" % (uuid.uuid4(), ext)
      # TODO: 'images' is hard coded
      return os.path.join('images', filename)
  
  class Photo(models.Model):
      post = models.ForeignKey(Post, related_name='photos', null=True, blank=True)
      image = models.ImageField(upload_to=get_unique_image_file_path)
  
      def get_image_abs_path(self):
          return os.path.join(settings.MEDIA_ROOT, self.image.name)
  </code></pre>
  
  <p>... and my serializer:</p>
  
  <pre><code>class PhotoSerializer(serializers.ModelSerializer):
      image = serializers.Field('image.url')
  
  class Meta:
      model = Photo
  </code></pre>
  
  <h2>Reproducing the bug</h2>
  
  <p>To generate the behavior, I POST to the server using the following <code>curl</code> command (exactly the same thing happens with my Android client code):</p>
  
  <pre><code>curl --form image=@test_image.jpg http://localhost:8000/rest_tutorial/photos
  </code></pre>
  
  <p>Looking into <code>generics.ListCreateAPIView</code>, the <code>create()</code> method looks like this:</p>
  
  <pre><code># Copied from rest_framework.mixins.CreateModelMixin 
  
  def create(self, request, *args, **kwargs):
      serializer = self.get_serializer(data=request.DATA, files=request.FILES)
  
      if serializer.is_valid():
          self.pre_save(serializer.object)
          self.object = serializer.save(force_insert=True)
          self.post_save(self.object, created=True)
          headers = self.get_success_headers(serializer.data)
          return Response(serializer.data, status=status.HTTP_201_CREATED,
                          headers=headers)
  
      return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>As I step through the above code in PyCharm's debugger, I can clearly see my file in the serializer's <code>init_files</code> field after <code>serializer.get_serializer()</code>. However, serializer's <code>object</code> field has a bunch of tracebacks but no references to my image file. Maybe something is wrong here?  </p>
  
  <p>After <code>self.object = serializer.save(force_insert=True)</code> the record is created with an empty image field, the file is  not uploaded, and <code>self.object.image.file</code> just contains a traceback referring to a <code>ValueError</code>.</p>
  
  <p>Any ideas? Thanks!</p>