'How to add data to manytomanyfield in drf

enter image description here

enter image description here

enter image description here

So I have a 2 models with a manytomany field. A book might be created without a publisher. When a publisher is added, I want to add it to the Books model with respect to its id any help is much appreciated



Solution 1:[1]

Suppose A is ManyToManyField and AA is the model, then -

First crate the instance:

Object = AA.object.create(validated_data)

then:

Object.A.add(value_to_be_added)

Now this is the guess work. Write your code for a detailed answer.

You can also use remove method to remove data from manytomanyfield.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ranu Vijay