Deep3015 Deep3015 - 1 year ago 115
Python Question

Error while saving image using Django Rest Framework with angularjs

I have two models Blogs, Photo. In Photo model I have fields 'blogs' as foreign key to Blogs model.

def content_file_name(instance, filename):
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)
tags= models.ManyToManyField(Tags)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __unicode__(self):
return '{0}'.format(self.title)

class PhotoViewSet(viewsets.ModelViewSet):
serializer_class = PhotoSerializer

def perform_create(self,serializer):'id'))
return super(PhotoViewSet,self).perform_create(serializer)

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


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


{image: ["No file was submitted."]}

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

Answer Source

In js:

headers: { 'Content-Type': undefined}

This solves the problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download