Error while saving image using Django Rest Framework with angularjs
<p>I have two models Blogs, Photo. In Photo model I have fields 'blogs' as foreign key to Blogs model.</p>

<p>models.py:</p>

<pre><code>def content_file_name(instance, filename):
    custt=str(datetime.now())
    return '/'.join(['content', instance.blogs.slug,custt, filename])

class Photo(models.Model):
    blogs = models.ForeignKey(Blogs)
    image = models.ImageField(upload_to=content_file_name)

class Blogs(models.Model):
    author = models.ForeignKey(CustomUser)
    title=models.CharField(max_length=100)
    postedin=models.ForeignKey(Posted)
    tags= models.ManyToManyField(Tags)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    slug=models.SlugField(max_length=255,unique=True)

    def __unicode__(self):
        return '{0}'.format(self.title)
</code></pre>

<p>views.py:</p>

<pre><code>class PhotoViewSet(viewsets.ModelViewSet):
    queryset=Photo.objects.all()
    serializer_class = PhotoSerializer

    def perform_create(self,serializer):
        serializer.save(blogs=Blogs.objects.latest('id'))
        return super(PhotoViewSet,self).perform_create(serializer)
</code></pre>

<p>serializers.py:</p>

<pre><code>class PhotoSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(
    max_length=None, use_url=True,
    )
    class Meta:
        model = Photo
        read_only_fields = ("blogs",)
</code></pre>

<p>js:</p>

<pre><code>function uploadpic(image) {
var fd = new FormData();
fd.append( 'image', image );
  return $http.post('/api/v1/uploadpic/',fd, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
});
}
</code></pre>

<p>result:</p>

<p>{image: ["No file was submitted."]}</p>

<p>Also I want to know, is there a better way to get blogs value (Foreign Key) for Photo Model.</p>

<p>In js:</p>

<pre><code>headers: { 'Content-Type': undefined}
</code></pre>

<p>This solves the problem.</p>

python angularjs django django-rest-framework