How do upload an Image to django from angularjs client using django rest framework? -
i can't figure out how should post image angularjs. i'm newbie in django , django-rest-framework. in advance.
models.py
class userimages(models.model): owner = models.foreignkey('auth.user', related_name='userimages') user=models.foreignkey(profile) # highlighted = models.textfield(default=none,blank=true,null=true) likes=jsonfield(null=true,blank=true) image=models.imagefield() pub_date=models.datetimefield(default=now) def __str__(self): return str(self.owner)
serializers.py
class imageserializer(serializers.modelserializer): owner = serializers.readonlyfield(source='user.username') image_url = serializers.serializermethodfield('gett_image_url') class meta: model=userimages fields=('id','owner','image','likes','image','image_url','owner') def gett_image_url(self,obj): return obj.image.url
views.py(what i've tried)
class profile_list(apiview): permission_classes = (permissions.isauthenticatedorreadonly,isownerorreadonly) parser_classes = (fileuploadparser,) def get(self, request, format=none): profiles=profile.objects.all() serializer=profileserializer(profiles, many=true) return response(serializer.data) def post(self, request, format=none): req=request.data image = req['display_image'] req['user']=self.request.user.id #self.request.user. serializer=profileserializer(data=req) if serializer.is_valid(): serializer.save(owner=self.request.user) return response(serializer.data,status=status.http_201_created) return response(serializer.errors,status=status.http_403_forbidden)
i can't figure out how should design view, read fileuploadparser going way on head code snippet of view nice. thank you.
Comments
Post a Comment