title: Django rest framework and Cloudinary
body: <p>Why have I got this error response: [02/Jan/2018 22:05:11] "POST /api/v1/images/ HTTP/1.1" 400 43 when I try to upload a new image.</p>
  
  <blockquote>
    <p>{ "error": "Not a valid string." }</p>
  </blockquote>
  
  <p>I completely new in Django world and a try to follow a tutorial but for some reason, my code didn't work and I try to debug my code but I can't understand why it does not work for me.</p>
  
  <p><strong>This my model</strong></p>
  
  <pre><code>from django.db import models
  from core.models import TimestampedModel
  
  class Image(TimestampedModel):
      image = models.CharField(max_length=350)
  
      def __str__(self):
          return self.image
  </code></pre>
  
  <p><strong>this my view</strong></p>
  
  <pre><code>from random import randint
  from rest_framework.response import Response
  from rest_framework.views import APIView
  from rest_framework import status
  from rest_framework.parsers import MultiPartParser, FormParser
  from cloudinary.templatetags import cloudinary
  from .serializers import ImageSerializer
  from .models import Image
  
  
  class ImageCloud(APIView):
      parser_classes = (MultiPartParser, FormParser,)
      serializer_class = ImageSerializer
  
      def get(self, request, format=None):
          images = Image.objects.all()
          serializer = ImageSerializer(images, many=True)
          return Response ({'images': serializer.data}, status=status.HTTP_200_OK)
  
      def upload_image_cloudinary(self, request, image_name):
          cloudinary.uploader.upload(
              request.FILES['image'],
              public_id=image_name,
              crop='limit',
              width='2000',
              height='2000',
              eager=[
                  {'width': 200, 'height': 200,
                    'crop': 'thumb', 'gravity ': 'auto',
                    'radius': 20, 'effect': 'sepia'},
                  {'width': 100, 'height': 150,
                   'crop': 'fit', 'format ': 'png'}
              ],
              tags=['image_ad', 'NAPI']
          )
  
      def post(self, request, format=None):
          serializer = self.serializer_class(data=request.data)
          if serializer.is_valid():
              try:
                  imageName = '{0}_v{1}'.format(request.FILES['image'].name.split('.')[0], randint(0, 100))
                  self.upload_image_cloudinary(request, imageName)
                  serializer.save(image_ad=imageName)
                  return Response(serializer.data, status=status.HTTP_201_CREATED)
              except Exception:
                  return Response({'image': 'Please upload a valid image'}, status=status.HTTP_400_BAD_REQUEST)
          else:
              print(serializer)
              return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p><strong>this my serializer</strong></p>
  
  <pre><code>from rest_framework import serializers
  from cloudinary.templatetags import cloudinary
  from django.contrib.humanize.templatetags.humanize import naturaltime
  from .models import Image
  
  class ImageSerializer(serializers.ModelSerializer):
      image = serializers.CharField(required=False)
      createdAt = serializers.SerializerMethodField(method_name='get_created_at')
      class Meta:
          model = Image
          fields = ('id', 'image', 'createdAt',)
  
      def to_representation(self, instance):
          representation = super(ImageSerializer, self).to_representation(instance)
          imageUrl = cloudinary.utils.cloudinary_url(
              instance.image, width=100, height=150, crop='fill')
  
          representation['image'] = imageUrl[0]
          representation['createdAt'] = naturaltime(instance.created)
          return representation
  
  
      def get_created_at(self, instance):
          return instance.created_at.isoformat()
  </code></pre>