I'm building an API with Applications and Servers. An application can have many servers and a server can have many applications.
These are my models.
class Server(Model):
    name = TextField(unique=True)
    description = TextField(blank=True)
    ip_address = TextField()
    class Meta:
        ordering = ["name"]
    def __str__(self):
        return self.name
class Application(Model):
    name = TextField(unique=True)
    description = TextField()
    is_critical = BooleanField()
    servers = ManyToManyField(Server, related_name="applications")
    class Meta:
        ordering = ["name"]
    def __str__(self):
        return self.name
These are my serializers.
class ServerSerializer(ModelSerializer):
    class Meta:
        model = Server
        fields = "__all__"
class ApplicationSerializer(ModelSerializer):
    servers = ServerSerializer(many=True, read_only=True)    
    class Meta:
        model = Application
        fields = "__all__"
I'm using DRF's Viewsets.
class ServerViewSet(ModelViewSet):
    queryset = Server.objects.all()
    serializer_class = ServerSerializer
class ApplicationViewSet(ModelViewSet):
    queryset = Application.objects.all()
    serializer_class = ApplicationSerializer
    @action(detail=True, methods=["HEAD", "GET", "POST", "DELETE"])
    def servers(self, request, pk=None):
        """Relate a server to the application"""
        application = self.get_object()
        if request.method in ("HEAD", "GET"):
            s_servers = ServerSerializer(application.servers, many=True)
            return Response(s_servers.data)
        else:
            server_id = request.data.get("id")
            if not server_id:
                return Response(
                    "Server details must be provided",
                    status=HTTP_400_BAD_REQUEST)
            server = get_object_or_404(Server, id=server_id)
            if request.method == "POST":
                application.servers.add(server)
                return Response(
                    "Server added to the application",
                    HTTP_201_CREATED)
            elif request.method == "DELETE":
                application.servers.remove(server)
                return Response(
                    "Server removed from the application",
                    HTTP_204_NO_CONTENT)
I would like servers to be unique and applications can have many servers. So, I will allow access to servers using
/api/servers and /api/servers/<int:pk>
I would like users to add existing servers to their applications using
/api/applications/app_id/servers
I'm able to achieve all operations except deleting a server that was already added to an application. How to handle the delete HTTP request using viewsets for the nested resource?