Django Rest Framework ImageField Django Rest Framework ImageField django django

Django Rest Framework ImageField


I think you can use request.data instead after django rest framework 3.0. The usage of request.DATA and request.FILES is now pending deprecation in favor of a single request.data attribute that contains all the parsed data.

You can check it from here


You seem to be missing the request.FILES argument to the serializer constructor in the your post and put handlers.

serializer = PhotoSerializer(data=request.DATA, files=request.FILES)


Uploading image files with Django Rest Framework:

models.py:

class MyPhoto(models.Model):    name = models.CharField(max_length=255)    image = models.ImageField(upload_to='myphoto/%Y/%m/%d/', null=True, max_length=255)

serializers.py:

class MyPhotoSerializer(serializers.ModelSerializer):    class Meta:        model = MyPhoto        fields = ('id', 'name', 'image')

views.py:

class PhotoList(APIView):    def post(self, request, format=None):        serializer = MyPhotoSerializer(data=request.data)        if serializer.is_valid():            serializer.save()            return Response(serializer.data, status=status.HTTP_201_CREATED)        else:            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Hope it helps someone.