M2M field doesn't get updated

I was trying to update an M2M field but it just doesn't get updated neither from admin panel nor the serializer! We have a "Book" model which has a "Category" field that is M2M to BookCategory Model

this is my model:

class Book(models.Model):
    category = models.ManyToManyField('BookCategory', related_name='bookCategory')

    def __str__(self):
        return self.name

class BookCategory(models.Model):
    name = models.CharField(max_length=100)
    parent = models.ForeignKey('BookCategory', on_delete=models.PROTECT, null=True, blank=True)
    description = models.TextField()

    def __str__(self):
        return self.name

and this is my view:

class BookChange(RetrieveUpdateDestroyAPIView):
    serializer_class = BookSerializer
    lookup_field = 'pk'

    def get_queryset(self):
        return Book.objects.all()

and the Model Serializer:

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'

The funny thing is that when I remove the current category it works but it doesn't add the new ones I also tried Overriding the serializer.save() in def update Can you help me out?

Back to Top