Given the following system
# Models
class Team(models.Model):
name = models.CharField(max_length=128)
class Player(models.Model):
name = models.CharField(max_length=128)
teams = models.ManyToManyField(Team)
# Serializers
class PlayerSerializer(serializers.ModelSerializer):
teams = serializers.PrimaryKeyRelatedField(many=True, queryset=League.objects.all(), required=False)
class Meta:
model = Team
fields = ('id', 'name', 'teams')
# Views
class TeamViewSet(viewsets.ModelViewSet):
queryset = Team.objects.all()
serializer_class = TeamSerializer
Basically a player can be in many teams
So the question is, how do I implement endpoints to manage the player-team relationship. Say we have two teams with ids 1, and 2 I create a player with
POST /players/ {'name': 'player1'} this player will have Id 1
I want to add player to teams 1 and 2, and then remove player from team 2
With this setup I can do PATCH /players/1/ {'teams': [1, 2]}
How do I now remove player1 from team 2?
Also, is using a patch request to add player1 to teams 1 and 2 the correct way to do this?