Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fengsp/django-grpc-framework
gRPC for Django.
https://github.com/fengsp/django-grpc-framework
django django-rest-framework grpc grpc-python
Last synced: 3 months ago
JSON representation
gRPC for Django.
- Host: GitHub
- URL: https://github.com/fengsp/django-grpc-framework
- Owner: fengsp
- License: apache-2.0
- Created: 2020-05-02T04:36:51.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-12-10T18:36:33.000Z (almost 2 years ago)
- Last Synced: 2024-06-18T17:10:46.286Z (5 months ago)
- Topics: django, django-rest-framework, grpc, grpc-python
- Language: Python
- Homepage: https://djangogrpcframework.readthedocs.io/
- Size: 88.9 KB
- Stars: 379
- Watchers: 20
- Forks: 50
- Open Issues: 28
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
- awesome-grpc - django-grpc-framework - A gRPC toolkit for Django inspired by djangorestframework (Language-Specific / Python)
README
Django gRPC Framework
=====================.. image:: https://img.shields.io/pypi/v/djangogrpcframework.svg
:target: https://img.shields.io/pypi/v/djangogrpcframework.svg.. image:: https://readthedocs.org/projects/djangogrpcframework/badge/?version=latest
:target: https://readthedocs.org/projects/djangogrpcframework/badge/?version=latest.. image:: https://travis-ci.org/fengsp/django-grpc-framework.svg?branch=master
:target: https://travis-ci.org/fengsp/django-grpc-framework.svg?branch=master.. image:: https://img.shields.io/pypi/pyversions/djangogrpcframework
:target: https://img.shields.io/pypi/pyversions/djangogrpcframework.. image:: https://img.shields.io/pypi/l/djangogrpcframework
:target: https://img.shields.io/pypi/l/djangogrpcframeworkDjango 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, proto3Installation
------------.. code-block:: bash
$ pip install djangogrpcframeworkAdd ``django_grpc_framework`` to ``INSTALLED_APPS`` setting:
.. code-block:: python
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:.. code-block:: bash
$ django-admin startproject demo
$ python manage.py migrateGenerate ``.proto`` file demo.proto_:
.. _demo.proto: https://github.com/fengsp/django-grpc-framework/blob/master/examples/demo/demo.proto
.. code-block:: bash
python manage.py generateproto --model django.contrib.auth.models.User --fields id,username,email --file demo.proto
Generate gRPC code:
.. code-block:: bash
python -m grpc_tools.protoc --proto_path=./ --python_out=./ --grpc_python_out=./ ./demo.proto
Now edit the ``demo/urls.py`` module:
.. code-block:: python
from django.contrib.auth.models import User
from django_grpc_framework import generics, proto_serializers
import demo_pb2
import demo_pb2_grpcclass 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 = UserProtoSerializerurlpatterns = []
def grpc_handlers(server):
demo_pb2_grpc.add_UserControllerServicer_to_server(UserService.as_servicer(), server)That's it, we're done!
.. code-block:: bash
$ python manage.py grpcrunserver --devYou can now run a gRPC client to access the service:
.. code-block:: python
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='')