Skip to main content

Simplify gRPC server and client usage in Django apps.

Project description

django-simple-grpc

django-simple-grpc is a developer-friendly Django package that makes using gRPC in Django simple, fast, and automatic.

It supports both server and client code, and includes powerful Django management commands for generating .proto files and gRPC services from your existing models.


🚀 Features

  • ✅ Auto-generate .proto files from Django models
  • ✅ Compile .proto files to Python gRPC stubs using grpcio-tools
  • ✅ Run gRPC servers using a clean run_grpc command
  • ✅ Build gRPC clients easily with GRPCClient
  • ✅ Works with Django 3.2+ (up to 5.x)
  • ✅ Compatible with Python 3.7–3.12

📦 Installation

pip install django-simple-grpc

⚙️ Setup in Django

Add to your settings.py:

INSTALLED_APPS = [
    ...
    "django_simple_grpc",
]

Then add the gRPC configuration:

# gRPC settings
GRPC_SERVER_PORT = 50051
GRPC_SERVER_ADDRESS = "localhost:50051"

# Replace <your_app> and <YourServiceName> with your actual values
GRPC_SERVICE_REGISTER = "grpc_generated.<your_app>_pb2_grpc.add_<YourServiceName>Servicer_to_server"
GRPC_SERVICE_IMPL = "<your_app>.grpc_service.<YourServiceName>Servicer"

🔁 Example if your app is called store and your service is ProductService:

GRPC_SERVICE_REGISTER = "grpc_generated.store_pb2_grpc.add_ProductServiceServicer_to_server"
GRPC_SERVICE_IMPL = "store.grpc_service.ProductServiceServicer"

🛠️ Basic Usage

1. Generate .proto file from your model

python manage.py grpc_auto_proto <app_name> <ServiceName>

Example:

python manage.py grpc_auto_proto book BookService

2. Compile the .proto into Python gRPC code

python manage.py grpc_generate

This creates Python files in grpc_generated/.


3. Implement the service logic

# book/grpc_service.py

from grpc_generated import book_pb2, book_pb2_grpc
from book.models import Book
from google.protobuf import empty_pb2

class BookServiceServicer(book_pb2_grpc.BookServiceServicer):
    def ListBooks(self, request, context):
        books = Book.objects.all()
        return book_pb2.BookList(
            items=[
                book_pb2.Book(id=b.id, title=b.title, author=b.author)
                for b in books
            ]
        )

4. Run the gRPC server

python manage.py run_grpc

🛰️ Client Example

Create a simple test script like test_client.py:

import os
import sys

# Add grpc_generated/ to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "grpc_generated"))

# Setup Django settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourproject.settings")
import django
django.setup()

from grpc_generated import book_pb2, book_pb2_grpc
from google.protobuf.empty_pb2 import Empty
from django_simple_grpc.client import GRPCClient

client = GRPCClient(book_pb2_grpc.BookServiceStub)
response = client.call("ListBooks", Empty())

for book in response.items:
    print(f"{book.id}: {book.title} by {book.author}")

🧪 Test the Full Flow

Terminal 1 – Start the gRPC server

python manage.py run_grpc

Terminal 2 – Run the test client

python test_client.py

✅ You should see a list of books from your Django database via gRPC!


📄 License

This project is open-source under the MIT License with an attribution clause.
You are free to use it, but please give credit and do not rebrand or publish it under your own name.


💬 Contributions

Pull requests and feedback are welcome!
Fork it, try it, and use it in your Django gRPC apps.


Made with ❤️ by HF

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_simple_grpc-0.1.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

django_simple_grpc-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file django_simple_grpc-0.1.1.tar.gz.

File metadata

  • Download URL: django_simple_grpc-0.1.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for django_simple_grpc-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f93e8679bce9927b2e1141ded2bb299f5078177a72efec47aa2c2b6cb4697606
MD5 48cf4cc7209fae917bc06d7c9f941d00
BLAKE2b-256 f7a3965341000bee424ec6ac0995ccdc2bd9e4d7a633383273aed7017c11a294

See more details on using hashes here.

File details

Details for the file django_simple_grpc-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_simple_grpc-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ebaf3b7fb52dea2085b8188201009d01c6772b8714da5f27e0e8f793d5e4e42
MD5 6498a1e9bb3410d2cc11aeb094be2468
BLAKE2b-256 61c269ddddd03cb67f2232a0c7ed9cfe806bc38b9c30b07d953f8d4979b05237

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