Skip to main content

gRPC for Django.

Project description

https://img.shields.io/pypi/v/django-grpc-framework-plus.svg https://readthedocs.org/projects/django-grpc-framework-plus/badge/?version=latest https://travis-ci.org/fengsp/django-grpc-framework.svg?branch=master https://img.shields.io/pypi/pyversions/django-grpc-framework-plus https://img.shields.io/pypi/l/django-grpc-framework-plus

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 django-grpc-framework-plus

Add django_grpc_framework_plus_plus to INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'django_grpc_framework_plus',
]

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_plus 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

django_grpc_framework_plus-0.1.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_grpc_framework_plus-0.1.0-py2.py3-none-any.whl (21.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_grpc_framework_plus-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_grpc_framework_plus-0.1.0.tar.gz
Algorithm Hash digest
SHA256 523ec3047c1783accd3fdebfea2cf47f488657bae4e743ca2142c2cf5a7b09a0
MD5 b08f86d061bf0ff90b8f7706e10b827c
BLAKE2b-256 f0aeef0464c2103062f1dd89ccb707a039a23ef1101a242ba0197a65a3360f58

See more details on using hashes here.

File details

Details for the file django_grpc_framework_plus-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_grpc_framework_plus-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1ac47a8f2f35026670c5dd19237d349d2170d35d4a6b46c436ffdd200fa23a21
MD5 c2a517209f5262307afa5d4e29eefeef
BLAKE2b-256 78324a33c59d3bc2994e20e0b1ee56c946faa493f3fbce52bbffadf6903585a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page