gRPC for Django.
Project description
Django gRPC framework is a toolkit for building gRPC services, inspired by djangorestframework.
Requirements
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
Installation
$ pip install djangogrpcframework
Add django_grpc_framework to INSTALLED_APPS setting:
INSTALLED_APPS = [ ... 'django_grpc_framework', ]
Demo
Here is a quick example of using gRPC framework to build a simple model-backed service for accessing users, startup a new project:
$ django-admin startproject demo
$ python manage.py migrate
Generate .proto file demo.proto:
python manage.py generateproto --model django.contrib.auth.models.User --fields id,username,email --file demo.proto
Generate gRPC code:
python -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ ./demo.proto
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!
$ python manage.py grpcrunserver --dev
You can now run a gRPC client to access the service:
with grpc.insecure_channel('localhost:50051') as channel:
stub = demo_pb2_grpc.UserControllerStub(channel)
for user in stub.List(demo_pb2.UserListRequest()):
print(user, end='')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
djangogrpcframework-0.2.tar.gz
(13.9 kB
view hashes)
Built Distribution
Close
Hashes for djangogrpcframework-0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6a2d99f5d7eb158912f721abdd62646429b2e141589e2364ab3b7cf1c32a708 |
|
MD5 | bfbcfff9ddec61d3a09db9755e040f22 |
|
BLAKE2b-256 | 1afa4f86d9fe39829407d60e070f1c5af5b7c86670cfd3cb90a260f4007d1182 |