Skip to main content

gRPC for Django.

Project description

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

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.1.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

djangogrpcframework-0.2.1-py2.py3-none-any.whl (21.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file djangogrpcframework-0.2.1.tar.gz.

File metadata

  • Download URL: djangogrpcframework-0.2.1.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.3

File hashes

Hashes for djangogrpcframework-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f96ae98d463f02745dbac5c18899801b738c79efd198e8cc709a7476533aec97
MD5 82e4bf47fa756033205f7b1806d84357
BLAKE2b-256 96d974504f99dbfe1955a3f5e0ca2952cc4a7decee156822f18f0d0cf3c58328

See more details on using hashes here.

File details

Details for the file djangogrpcframework-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: djangogrpcframework-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.3

File hashes

Hashes for djangogrpcframework-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1835bd87ac9bdbdbdb631cd1c12720e711260d316303d19e4c63e53896ff0e6
MD5 fff54d94fd0c847b9b12445cb2287c6a
BLAKE2b-256 ae899a475e234786ba14fd3b538a18df0d20e98ce5a51906e1bd4485a74164e1

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