title: Django Rest Framework ImageField
body: <p>I can not save the image in this ImageField.</p>
  
  <p><strong>when sending data back:</strong></p>
  
  <pre><code>{
      "image": ["No file was submitted. Check the encoding type on the form."]
  }
  </code></pre>
  
  <p><strong>model.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>class MyPhoto(models.Model):
      owner = models.ForeignKey('auth.User', related_name='image')
      image = models.ImageField(upload_to='photos', max_length=254)
  </code></pre>
  
  <p><strong>serializers.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>class PhotoSerializer(serializers.HyperlinkedModelSerializer):
      class Meta:
          model = MyPhoto
          fields = ('url', 'id', 'image', 'owner')
          owner = serializers.Field(source='owner.username')
  </code></pre>
  
  <p><strong>view.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>class PhotoList(APIView):
      permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly)
  
      def get(self, request, format=None):
          photo = MyPhoto.objects.all()
          serializer = PhotoSerializer(photo, many=True)
          return Response(data=serializer.data, status=status.HTTP_200_OK)
  
      def post(self, request, format=None):
         serializer = PhotoSerializer(data=request.DATA)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data, status=status.HTTP_201_CREATED)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  
      def pre_save(self, obj):
          obj.owner = self.request.user
  
  
  class PhotoDetail(APIView):
  
      permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly)
  
      def get_object(self, pk):
          try:
              return MyPhoto.objects.get(pk=pk)
          except MyPhoto.DoesNotExist:
              raise Http404
  
      def get(self, request, pk, format=None):
          photo = self.get_object(pk)
          serializer = PhotoSerializer(photo)
          return Response(serializer.data)
  
      def put(self, request, pk, format=None):
          photo = self.get_object(pk)
          serializer = PhotoSerializer(photo, data=request.DATA)
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  
      def delete(self, request, pk, format=None):
          photo = self.get_object(pk)
          photo.delete()
          return Response(status=status.HTTP_204_NO_CONTENT)
  
      def pre_save(self, obj):
          obj.owner = self.request.user
  </code></pre>
  
  <p><strong>url.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>urlpatterns = patterns('',
      url(r'^$', 'main.views.main_page'),
      url(r'^api/photo/$', views.PhotoList.as_view(), name='myphoto-list'),
      url(r'^api/photo/(?P&lt;pk&gt;[0-9]+)/$', views.PhotoDetail.as_view(), name='myphoto-detail'),)
  </code></pre>
  
  <p><strong>curl</strong></p>
  
  <pre><code>curl -X POST -S \
    -H 'Content-Type: application/json' \
    -u "michael:bush_pass" \
    --data-binary '{"owner":"/users/1/", \
      "image":"/Users/test/Downloads/1383310998_05.jpg"}' \
    127.0.0.1:8000/api/photo/
  </code></pre>