ajax - angularjs http post image file to drf rest interface got error: AttributeError: 'OrderedDict' object has no attribute 'pk' -
i creating system consisting angularjs frontend web app , drf rest interface. can use command line 'curl' upload image file drf rest interface, when use angularjs http post, got error message: 'ordereddict' object has no attribute 'pk'
my drf setup follows:
models.py: def upload_to(instance, filename): return 'post_image/{0}/{1}'.format(instance.author.id, filename) class postimage(models.model): author = models.foreignkey(author, blank=false, editable=false, related_name='images') post = models.foreignkey(post, blank=false, editable=false, related_name='images') image = models.imagefield(_('image'), blank=true, null=true, upload_to=upload_to) created = models.datetimefield(editable=false) updated = models.datetimefield(editable=false) def __unicode__(self): return self.name def save(self, *args, **kwargs): ''' on save, update timestamps ''' if not self.id: self.created = timezone.now() self.updated = timezone.now() return super(postimage, self).save(*args, **kwargs) serializers.py class postimageserializer(serializers.hyperlinkedmodelserializer): author = serializers.hyperlinkedrelatedfield(read_only=true, view_name='author-detail') post = serializers.hyperlinkedrelatedfield(read_only=true, view_name='post-detail') class meta: model = postimage fields = ('url', 'image', 'author', 'post', 'created', 'updated') views.py class postimageview(generics.listcreateapiview): """ list , create post image endpoint allowed request method: get, post """ serializer_class = postimageserializer permission_classes = [permissions.isauthenticated] parser_classes = (formparser, multipartparser, fileuploadparser,) def get_queryset(self): queryset = super(postimageview, self).get_queryset() return queryset.filter(post__pk=self.kwargs['pk']) def perform_create(self, serializer): post = post.objects.get(pk=self.kwargs['pk']) if 'upload' in self.request.data: file_obj = self.request.data['upload'] serializer.save(author=self.request.user, post=post, image=file_obj) return response(status=status.http_201_created) else: return response(status=status.http_400_bad_request) using jwt authentication: rest_framework = { 'default_authentication_classes': ( 'rest_framework_jwt.authentication.jsonwebtokenauthentication', ) , }
log error message:
internal server error: /api/v1/post/22/image traceback (most recent call last): file "/root/rest_drf/env/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) file "/root/rest_drf/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) file "/root/rest_drf/env/local/lib/python2.7/site-packages/django/views/generic/base.py", line 71, in view return self.dispatch(request, *args, **kwargs) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/views.py", line 452, in dispatch response = self.handle_exception(exc) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/views.py", line 449, in dispatch response = handler(request, *args, **kwargs) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/generics.py", line 244, in post return self.create(request, *args, **kwargs) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/mixins.py", line 21, in create headers = self.get_success_headers(serializer.data) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 466, in data ret = super(serializer, self).data file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 215, in data self._data = self.to_representation(self.validated_data) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 435, in to_representation ret[field.field_name] = field.to_representation(attribute) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/relations.py", line 264, in to_representation return self.get_url(value, self.view_name, request, format) file "/root/rest_drf/env/local/lib/python2.7/site-packages/rest_framework/relations.py", line 199, in get_url if obj.pk none: attributeerror: 'ordereddict' object has no attribute 'pk' [pid: 29484|app: 0|req: 2/4] 76.114.185.25 () {54 vars in 1242 bytes} [sun jan 24 16:41:13 2016] post /api/v1/post/22/image => generated 107983 bytes in 153 msecs (http/1.1 500) 2 headers in 92 bytes (1 switches on core 1)
Comments
Post a Comment