How to add data into ManyToMany field with Python Django?

Sometimes, we want to add data into ManyToMany field with Python Django.

In this article, we’ll look at how to add data into ManyToMany field with Python Django.

How to add data into ManyToMany field with Python Django?

To add data into ManyToMany field with Python Django, we can use the add method.

For instance, we write

my_obj.categories.add(fragmentCategory.objects.get(id=1))

to add the category entry related to my_obj by calling add with

fragmentCategory.objects.get(id=1)

This will add the entry for the association table between my_obj and categories.

Conclusion

To add data into ManyToMany field with Python Django, we can use the add method.