title: Django Rest Framework - Updating related model using ModelSerializer and ModelViewSet
body: <h2>BACKGROUND</h2>
  
  <p>I have two serializers: <strong>PostSerializer</strong> and <strong>PostImageSerializer</strong> which both inherit DRF ModelSerializer. The PostImage model is linked with Post by related_name='photos'. </p>
  
  <p>Since I want the serializer to perform <strong>update</strong>, PostSerializer overrides update() method from ModelSerializer as stated in official DRF doc.</p>
  
  <pre><code>class PostSerializer(serializers.ModelSerializer):
      photos = PostImageSerializer(many=True)
  
      class Meta:
          model = Post
          fields = ('title', 'content')
  
      def update(self, instance, validated_data):
          photos_data = validated_data.pop('photos')
          for photo in photos_data:
              PostImage.objects.create(post=instance, image=photo)
          return super(PostSerializer, self).update(instance, validated_data)
  
  class PostImageSerializer(serializer.ModelSerializer):
      class Meta:
          model = PostImage
          fields = ('image', 'post')
  </code></pre>
  
  <p>I have also defined a <strong>ViewSet</strong> which inherits ModelViewSet. </p>
  
  <pre><code> class PostViewSet(viewsets.ModelViewSet):
          queryset = Post.objects.all()
          serializer_class = PostSerializer
  </code></pre>
  
  <p>Finally the PostViewSet is registered to DefaultRouter. (Omitted code)</p>
  
  <h2>Goal</h2>
  
  <p>The goals are simple.</p>
  
  <ul>
  <li>Send PUT request via PostMan with url like 'PUT <a href="http://localhost:8000/api/posts/1/" rel="noreferrer">http://localhost:8000/api/posts/1/</a>'</li>
  <li>Since Image files should be included, request would be done by form-data like <a href="https://i.stack.imgur.com/2Tg3n.png" rel="noreferrer">below.</a></li>
  </ul>
  
  <h2> Problem </h2>
  
  <p>I'm getting 400 Response with error message as following.</p>
  
  <blockquote>
    <p>{<br>
        "photos": [
            "This field is required."
        ],<br>
        "title": [
            "This field is required."
        ],<br>
        "content": [
            "This field is required."
        ]<br>
    }</p>
  </blockquote>
  
  <p>(Should you plz note that the error messages might not exactly fit with DRF error messages since they are translated.)</p>
  
  <p>It is obvious that none of my PUT fields are applied. 
  So I have been digging around Django rest framework source code itself and found out serializer validation in ViewSet <a href="https://i.stack.imgur.com/2CAzA.png" rel="noreferrer">update() method continues to fail</a>. </p>
  
  <p>I doubt that because I PUT request not by JSON but by form-data using key-value pair so request.data is not properly validated. </p>
  
  <p>However, I should contain multiple images in the request which means plain JSON would not work.</p>
  
  <p>What would be the most clear solutions for this case?</p>
  
  <p>Thank you.</p>
  
  <h2> Update </h2>
  
  <p>As Neil pointed out, I added print(self) at the first line of update() method of PostSerializer. However nothing printed out on my console. </p>
  
  <p>I think this is due to my doupt above because perform_update() method which calls serializer update() method is called <strong>AFTER</strong> <a href="https://i.stack.imgur.com/2CAzA.png" rel="noreferrer">serializer is validated</a>.</p>
  
  <p>Therefore the main concept of my question could be narrowed to the followings.</p>
  
  <ol>
  <li>How should I fix requested data fields so that validation inside update() method of ModelViewSet could pass?</li>
  <li>Do I have to override update() method of ModelViewSet(not the one from ModelSerializer)?</li>
  </ol>
  
  <p>Thanks again.</p>