title: Django rest framework how to create 3 nested-serializer objects?
body: <p>I'm trying to create nested objects in Django Rest Framework according to the <a href="https://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers" rel="nofollow noreferrer">docs</a>.</p>
  <p>this my models.py :</p>
  <pre><code>from django.db import models
  
  
  class Request(models.Model):
      origin = models.CharField(max_length=255, blank=True)
  
      def __str__(self):
          return str(self.id)
  
  
  class RequestOrders(models.Model):
      request_id = models.ForeignKey(Request, related_name='request_orders', on_delete=models.DO_NOTHING, blank=True, null=True)
      position = models.CharField(max_length=255, blank=True)
      destination = models.CharField(max_length=255, blank=True)
      order_ref = models.CharField(max_length=255, blank=True)
  
      def __str__(self):
          return self.id
  
  
  class RequestOrderItemss(models.Model):
      request_order_id = models.ForeignKey(RequestOrders, related_name='request_order_itemss', on_delete=models.DO_NOTHING, blank=True, null=True)
      product_id = models.ForeignKey('product.Product', on_delete=models.DO_NOTHING, blank=True, null=True)
      qty = models.CharField(max_length=255, blank=True)
  
      def __str__(self):
          return self.id
  </code></pre>
  <p>Here's my serializers.py :</p>
  <pre><code>from rest_framework import serializers
  from request.models import Request, RequestOrders, RequestOrderItemss
  from product.serializers import ProductSerializer
  
  
  class RequestOrderItemssSerializer(serializers.ModelSerializer):
      class Meta:
          model = RequestOrderItemss
          fields = ('id', 'request_order_id', 'product_id', 'qty')
  
  
  class RequestOrdersSerializer(serializers.ModelSerializer):
      request_order_itemss = RequestOrderItemssSerializer(many=True)
  
      class Meta:
          model = RequestOrders
          fields = ('id', 'request_id', 'position', 'destination', 'order_ref', 'request_order_itemss')
  
  
  class RequestSerializer(serializers.ModelSerializer):
      request_orders = RequestOrdersSerializer(many=True)
  
      class Meta:
          model = Request
          fields = ('id', 'origin', 'request_orders')
  
      def create(self, validated_data):
          request_orders_data = validated_data.pop('request_orders')
          request = Request.objects.create(**validated_data)
          for request_order_data in request_orders_data:
              request_order_items_data = request_order_data.pop('request_order_itemss')
              request_order = RequestOrders.objects.create(request_id=request,
                                                                          **request_order_data)
              for request_order_item_data in request_order_items_data:
                  RequestOrderItemss.objects.create(request_order_id=request_order,
                                                           **request_order_item_data)
          return request
  
      def update(self, instance, validated_data):
          request_orders_data = validated_data.pop('request_orders')
          orders = instance.request_orders.all()
          orders = list(orders)
          instance.origin = validated_data.get('origin', instance.origin)
          instance.save()
  
          for request_order_data in request_orders_data:
              request_order_items_data = request_order_data.pop('request_order_itemss')
              items = instance.request_orders.get().request_order_itemss.all()
              items = list(items)
              for request_order_item_data in request_order_items_data:
                  item = items.pop(0)
                  item.product_id = request_order_item_data.get('product_id', item.product_id)
                  item.qty = request_order_item_data.get('qty', item.qty)
                  item.save()
              order = orders.pop(0)
              order.position = request_order_data.get('position', order.position)
              order.destination = request_order_data.get('destination', order.destination)
              order.order_ref = request_order_data.get('order_ref', order.order_ref)
              order.save()
          return instance
  </code></pre>
  <p>and this is my views.py</p>
  <pre><code>from django.http import JsonResponse
  from rest_framework import generics
  from rest_framework.permissions import IsAuthenticated
  from request.models import Request, RequestOrders, RequestOrderItemss
  from request.serializers import RequestSerializer, RequestOrdersSerializer, \
      RequestOrderItemssSerializer
  
  
  class RequestListView(generics.ListCreateAPIView):
      queryset = Request.objects.all()
      serializer_class = RequestSerializer
      permission_classes = (IsAuthenticated,)
  
  
  class RequestView(generics.RetrieveUpdateDestroyAPIView):
      serializer_class = RequestSerializer
      queryset = Request.objects.all()
      permission_classes = (IsAuthenticated,)
  
  
  class RequestOrdersListView(generics.ListCreateAPIView):
      queryset = RequestOrders.objects.all()
      serializer_class = RequestOrdersSerializer
      permission_classes = (IsAuthenticated,)
  
  
  class RequestOrdersView(generics.RetrieveUpdateDestroyAPIView):
      serializer_class = RequestOrdersSerializer
      queryset = RequestOrders.objects.all()
      permission_classes = (IsAuthenticated,)
  
  
  class RequestOrderItemssListView(generics.ListCreateAPIView):
      queryset = RequestOrderItemss.objects.all()
      serializer_class = RequestOrderItemssSerializer
      permission_classes = (IsAuthenticated,)
  
  
  class RequestOrderItemssView(generics.RetrieveUpdateDestroyAPIView):
      serializer_class = RequestOrderItemssSerializer
      queryset = RequestOrderItemss.objects.all()
      permission_classes = (IsAuthenticated,)
  </code></pre>
  <p>this is my JSON data that i want to save in data base :</p>
  <pre><code>{
  &quot;origin&quot;: &quot;Krishna Rungta&quot;,
  &quot;request_orders&quot;: [
      {
          &quot;position&quot;:&quot;2&quot;,
          &quot;destination&quot; :&quot;Paris&quot;,
          &quot;order_ref&quot;:&quot;ref#123&quot;,
          &quot;request_order_itemss&quot;:[
              {
                  &quot;product_id&quot;:4,
                  &quot;qty&quot;:444,
              }
              {
                  &quot;product_id&quot;:5,
                  &quot;qty&quot;:33,
              }
          ]
      }
      {
          &quot;position&quot;:&quot;3&quot;,
          &quot;destination&quot; :&quot;Paris&quot;,
          &quot;order_ref&quot;:&quot;ref#124&quot;,
          &quot;request_order_itemss&quot;:[
              {
                  &quot;product_id&quot;:6,
                  &quot;qty&quot;:123,
              }
              {
                  &quot;product_id&quot;:4,
                  &quot;qty&quot;:54,
              }
          ]
      }
  ]
  </code></pre>
  <p>}</p>
  <p>when I try to test this on Postman I get this error :</p>
  <blockquote>
  <p>Direct assignment to the reverse side of a related set is prohibited.
  Use request_order_itemss.set() instead.</p>
  </blockquote>
  <p>So I want to know where is the problem and is what i did is right or is there something wrong.</p>