I have the following code. It allows uploading an Image using Rest API's in Django. How ever due to the nature of this class I am unable to get the user the way it's usually done (request.user)
class PhotoViewSet(viewsets.ModelViewSet, APIView):
permission_classes = (permissions.IsAuthenticated,)
queryset = Photo.objects.all()
serializer_class = PhotoSerializer
@receiver(post_save, sender=Photo)
def my_handler(sender, instance, **kwargs):
print request.user.username #How to make this work?
router = routers.SimpleRouter()
router.register(r'avatar', PhotoViewSet, base_name='love_stackoverflow')
如果要将用户添加到Photo
实例,请使用PhotoSerializer中的CurrentUserDefault
(作为只读字段),或将用户传递到视图的perform_create
的序列化器的save
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.