title: django rest framework update model with image
body: <p>Is there a way to update a django model with a versatile image field via django rest framework without losing the original image? The problem is that the serializer don't accept an image url, just an image encoded in base64.</p>
  
  <p><strong>About the <em>"update a django model with a versatile image field via django rest framework without losing the original image"</em></strong></p>
  
  <p>I'm referring about the django lib <code>versatile image field</code>, I use it in some of my models because I need to give some size options for the images. The proble is, when I first upload a image on the <code>front-end</code>, it send an image in base 64 format. But when I'm going to update a model, drf send me the image as an url, so, when the data is posted to the <code>back-end</code>, drf ignores the url on the json an delete the already uploaded image.</p>
  
  <p><strong><em>serializers.py</em></strong></p>
  
  <pre><code>class ProductImageSerializer(serializers.ModelSerializer):
      source = Base64ImageField(source='image', required=False, )
      original = Base64ImageField(source='image', read_only=True)
      crop = VersatileImageFieldSerializer(sizes=[
          ('original', 'url'),
          ('small', 'thumbnail__150x150'),
          ('medium', 'thumbnail__450x450'),
      ], source='image', read_only=True)
  
      class Meta:
          model = ProductImage
          fields = ('crop', 'source', 'original',)
  
  
  class ProductSerializer(serializers.ModelSerializer):
      images = ProductImageSerializer(many=True)
      variants = ProductVariantSerializer(many=True)
      permissions_classes = (permissions.IsAuthenticatedOrReadOnly,)
      price = serializers.CharField(required=False)
      sector = CreatableSlugRelatedField(slug_field='name', queryset=Sector.objects.all(),
                                         required=False)
  
      class Meta:
          model = Product
          fields = ('id', 'small_name', 'large_name', 'images', 'variants', 'price',
                    'description', 'promo_tabloid', 'stock', 'sector')
  
      def update(self, instance, validated_data):
              if self.is_valid():
                  images_data = validated_data.pop('images')
                  variants_data = validated_data.pop('variants')
                  for key in validated_data:
                      try:
                          instance.__dict__[key] = validated_data[key]
                      except KeyError:
                          pass
  
                  new_variants = []
                  for variant_data in variants_data:
                      print variant_data
                      variant, _ = Variant.objects.get_or_create(product=instance, value=variant_data.get('value'))
                      new_variants.append(variant)
  
                  new_images = []
                  print images_data
                  for image_data in images_data:
                      image = ProductImage.get_or_create(product=instance, image=image_data.get('image'))
                      new_images.append(image)
  
                  try:
                      with transaction.atomic():
                          Variant.objects.filter(product=instance).delete()
                          Variant.objects.bulk_create(new_variants)
  
                          ProductImage.objects.filter(product=instance).delete()
                          ProductImage.objects.bulk_create(new_images)
                  except IntegrityError:
                      return {u'error': u'Failed'}
                  instance.save()
                  return instance
              else:
                  return self.errors
  </code></pre>