I have a model with ManyToMany relationship.
class File(models.Model):
name = models.CharField(max_length=64)
def __str__(self):
return self.name
class Folder(models.Model):
name = models.CharField(max_length=64)
files = models.ManyToManyField(File, related_name='folders', default=None)
def __str__(self):
return self.name
Serializers:
class FileSerializer(serializers.ModelSerializer):
class Meta:
model = models.File
fields = '__all__'
class FolderSerializer(serializers.ModelSerializer):
files = FileSerializer(many=True, read_only=True)
file = serializers.PrimaryKeyRelatedField(queryset=models.File.objects.all(),
write_only=True, label='File Name')
class Meta:
model = models.Folder
fields = ('id', 'name', 'files', 'file')
I am able to add a file object to the folder. I am able to update the name of the folder too. But how do I remove a file object from the folder?
If you are accessing from files
Then you can do
Edit: Your app's urls file:
urls.py:
Your apps views.py: