title: Django REST: Uploading and serializing multiple images
body: <p>I have 2 models <code>Task</code> and <code>TaskImage</code> which is a collection of images belonging to <code>Task</code> object.</p>
  
  <p>What I want is to be able to add multiple images to my <code>Task</code> object, but I can only do it using 2 models. Currently, when I add images, it doesn't let me upload them and save new objects.</p>
  
  <p><strong>settings.py</strong></p>
  
  <pre><code>MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  MEDIA_URL = '/media/'
  </code></pre>
  
  <p><strong>serializers.py</strong> </p>
  
  <pre><code>class TaskImageSerializer(serializers.ModelSerializer):
      class Meta:
          model = TaskImage
          fields = ('image',)
  
  
  class TaskSerializer(serializers.HyperlinkedModelSerializer):
      user = serializers.ReadOnlyField(source='user.username')
      images = TaskImageSerializer(source='image_set', many=True, read_only=True)
  
      class Meta:
          model = Task
          fields = '__all__'
  
      def create(self, validated_data):
          images_data = validated_data.pop('images')
          task = Task.objects.create(**validated_data)
          for image_data in images_data:
              TaskImage.objects.create(task=task, **image_data)
          return task
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>class Task(models.Model):
      title = models.CharField(max_length=100, blank=False)
      user = models.ForeignKey(User)
  
      def save(self, *args, **kwargs):
          super(Task, self).save(*args, **kwargs)
  
  class TaskImage(models.Model):
      task = models.ForeignKey(Task, on_delete=models.CASCADE)
      image = models.FileField(blank=True)
  </code></pre>
  
  <p>However, when I do a post request:</p>
  
  <p><a href="https://i.stack.imgur.com/NndxK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NndxK.png" alt="enter image description here"></a></p>
  
  <p>I get the following traceback:</p>
  
  <blockquote>
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/exception.py"
    in inner
      41.             response = get_response(request)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/base.py"
    in _get_response
      187.                 response = self.process_exception_by_middleware(e, request)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/core/handlers/base.py"
    in _get_response
      185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/django/views/decorators/csrf.py"
    in wrapped_view
      58.         return view_func(*args, **kwargs)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/viewsets.py"
    in view
      95.             return self.dispatch(request, *args, **kwargs)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py"
    in dispatch
      494.             response = self.handle_exception(exc)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py"
    in handle_exception
      454.             self.raise_uncaught_exception(exc)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/views.py"
    in dispatch
      491.             response = handler(request, *args, **kwargs)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/mixins.py"
    in create
      21.         self.perform_create(serializer)</p>
    
    <p>File "/Users/gr/Desktop/PycharmProjects/godo/api/views.py" in
    perform_create
      152.         serializer.save(user=self.request.user)</p>
    
    <p>File
    "/Applications/Anaconda/anaconda/envs/godo/lib/python3.6/site-packages/rest_framework/serializers.py"
    in save
      214.             self.instance = self.create(validated_data)</p>
    
    <p>File "/Users/gr/Desktop/PycharmProjects/godo/api/serializers.py" in
    create
      67.         images_data = validated_data.pop('images')</p>
    
    <p>Exception Type: KeyError at /api/tasks/ Exception Value: 'images'</p>
  </blockquote>