Django gRPC framework is a toolkit for building gRPC services, inspired by djangorestframework.
- Python (3.6, 3.7, 3.8)
- Django (2.2, 3.0), Django REST Framework (3.10.x, 3.11.x)
- gRPC, gRPC tools, proto3
Add django_grpc_framework
to INSTALLED_APPS
setting:
Here is a quick example of using gRPC framework to build a simple model-backed service for accessing users, startup a new project:
Generate .proto
file demo.proto:
Generate gRPC code:
Now edit the demo/urls.py
module:
from django.contrib.auth.models import User
from django_grpc_framework import generics, proto_serializers
import demo_pb2
import demo_pb2_grpc
class UserProtoSerializer(proto_serializers.ModelProtoSerializer):
class Meta:
model = User
proto_class = demo_pb2.User
fields = ['id', 'username', 'email']
class UserService(generics.ModelService):
queryset = User.objects.all()
serializer_class = UserProtoSerializer
urlpatterns = []
def grpc_handlers(server):
demo_pb2_grpc.add_UserControllerServicer_to_server(UserService.as_servicer(), server)
That's it, we're done!
You can now run a gRPC client to access the service: