title: Django REST Framework: serializing an ImageField
body: <p>I'm having huge issues with Django REST Framework and its serializers in relation to a model with an ImageField</p>
  
  <p>Here's the code for my model:</p>
  
  <pre><code>class Photo(models.Model):
      id = models.AutoField(primary_key=True)
      image_file = models.ImageField(upload_to=generate_image_filename_and_path,
                                     width_field="image_width",
                                     height_field="image_height",
                                     null=False,
                                     blank=False)
      image_width = models.IntegerField(null=False, blank=False)
      image_height = models.IntegerField(null=False, blank=False)
  </code></pre>
  
  <p>and here's the serializer:</p>
  
  <pre><code>class PhotoSerializer(serializers.ModelSerializer):
      class Meta:
          model = Photo
          fields = ('image_file',)
          read_only = ('id', 'image_width', 'image_height')
  </code></pre>
  
  <p>id, image_width and image_height are set as read_only fields, as I want them to be generated by the model class. ID is something I couldn't even provide when first creating a model instance.</p>
  
  <p>I've had much trouble. In the current state, I have a test that looks like this:</p>
  
  <pre><code>class PhotoSerializerTestCase(TestCase):
      def test_native_data_type_serialization(self):
          img_file = create_generic_image_file('test_img.jpg',
                                               'image/jpeg')
  
          p = Photo(image_file=img_file)
          p.save()
  
          serialization = PhotoSerializer(p)
  
          expected_id = p.id
          expected_image_width = p.image_width
          expected_image_height = p.image_height
  
          print(serialization)
          actual_id = serialization.data['id']
          actual_image_width = serialization.data['image_width']
          actual_image_height = serialization.data['image_height']
  
          self.assertEqual(expected_id, actual_id)
          self.assertEqual(expected_image_width, actual_image_width)
          self.assertEqual(expected_image_height, actual_image_height)
  </code></pre>
  
  <p>Here's the function that I call to create the generic image file for the test:</p>
  
  <pre><code>def create_generic_image_file(name, content_type):
  
      path = '\\test_files\\test_image_generic.jpg'
      file_path = BASE_DIR + path
      file_name = name
      content = open(file_path, 'rb').read()
  
      image_file = SimpleUploadedFile(name=file_name,
                                      content=content,
                                      content_type=content_type)
  
      return image_file
  </code></pre>
  
  <p>In present state, I'm left with an error as follows:</p>
  
  <pre><code>Creating test database for alias 'default'...
  ..PhotoSerializer(&lt;Photo: Photo object&gt;):
      image_file = ImageField()
  E.
  ======================================================================
  ERROR: test_native_data_type_serialization (photo_broker.test_serializers.PhotoSerializerTestCase)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "&lt;local_path&gt;\test_serializers.py", line 25, in test_native_data_type_serialization
      actual_id = serialization.data['id']
  KeyError: 'id'
  </code></pre>
  
  <p>I can't even recall all the different errors that I've gotten previously, but I'm sure they'll pop up when this one's cleared.</p>
  
  <p>edit:</p>
  
  <p>Ok, I cleared the key error by moving all the fields back to PhotoSerializer.Meta.fields instead of PhotoSerializer.Meta.read_only. Now I'm back to one of my original questions. Let me explain with a test:</p>
  
  <pre><code>def test_create_model_from_serializer(self):
      img_file = create_generic_image_file('test_img.jpg',
                                           'image/jpeg')
  
      data = {'image_file': img_file,
              'id': 7357,
              'image_width': 1337,
              'image_height': 715517}
  
      s = PhotoSerializer(data=data)
      print(type(s))
      print(s.is_valid())
      print(s)
  </code></pre>
  
  <p>This yields clearly likable output and the serializer validates without a cough, but this is not what I want to be able to do. The ID and the dimensions should be calculated by the model. Not to whatever values by the serializer.</p>
  
  <p>If I remove them from the fields attribute, then I can never get them into my serialization either. That's where I need them.</p>
  
  <p>Or am I getting confused here and there actually is a valid use case, where I originally create the model normally, without going through the serializer?</p>