title: Update ManytoMany relationship in Django Rest Framework
body: <p>In my django application I have a ManytoMany relationship between <code>Orders</code> and <code>Packages</code>. An order can have multiple packages. I want to know about the update and create methods</p>
  
  <p>Models.py</p>
  
  <pre><code>class Package(models.Model):
  
      prod_name = models.CharField(max_length=255, default=0)
      quantity = models.IntegerField(default=0)
      unit_price = models.IntegerField(default=0)
  
  class Orders(models.Model):
  
      order_id = models.CharField(max_length=255, default=0)
      package = models.ManyToManyField(Package)
      is_cod = models.BooleanField(default=False)
  </code></pre>
  
  <p>Serializers.py</p>
  
  <pre><code>class PackageSerializer(serializers.ModelSerializer):
      class Meta:
          model = Package
          fields = "__all__"
  
  class OrderSerializer(serializers.ModelSerializer):
      package = PackageSerializer(many=True)
  
      class Meta:
          model = Orders
          fields = "__all__"
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>class OrdersCreateAPIView(generics.CreateAPIView):
      permission_classes = (permissions.IsAuthenticated,)
      serializer_class = OrderSerializer
  
      def post(self, request):
  
          serializer = OrderSerializer(data=request.data)
          if serializer.is_valid():
              serializer.save()
              return Response(serializer.data, status=status.HTTP_201_CREATED)
          return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>Is that sufficient to handle the related data? I am trying to understand <code>ManytoMany</code> relationship both in Django as well as DRF so please explain if I need to change the Models or views in anyway </p>
  
  <p>Update:</p>
  
  <p>I have updated my serializer as well as view in order to create <code>manytomany</code> related objectslike this:</p>
  
  <pre><code>class OrderSerializer(serializers.ModelSerializer):
      package = PackageSerializer(many=True)
  
      class Meta:
          model = Orders
          fields = "__all__"
  
      def create(self, validated_data):
          package_data = validated_data.pop('package')
          pkgs = []
          order = Orders.objects.create(**validated_data)
          for i in package_data:
              try:
                  p = Package.objects.create(**i)
                  pkgs.append(p)
              except:
                  pass
          order.package.set(pkgs)
          return order
  </code></pre>
  
  <p>Views.py</p>
  
  <pre><code>class OrdersCreateAPIView(CreateAPIView):
      permission_classes = (permissions.IsAuthenticated,)
      serializer_class = OrderSerializer
  
      def perform_create(self,serializer):
          serializer.save(owner=self.request.user)
  </code></pre>
  
  <p>However I am still unclear about overriding the <code>update</code> method of <code>RetrieveUpdateDestroyAPIView</code>. Also, Is the above method is the right method to store <code>M2M</code> related objects ?</p>
  
  <p>Please help with the update part of the serializer, I understand that I have to pass the query in the serializer</p>