PUT request to Django API to update list of nested objects with Many To Many relationships

We are going to create UserProfile(AbstractUser) and using ManyToMany relation in it.
model.py
In this I am using many to many field by (“self”) but if you want to create other model you can create it will work fine.
serializers.py
Here I am using email in place of id . for that I have created NameSerializer . You can use id also it will work fine . the most important part is that update function . this update function work when put request id called . first it delete all the previous instances and then by the help of the serializer it take the data of many to many field . After that we are using for loop and one by one inserting all the data in that instance ., at last returning instance.
views.py