https://github.com/joeriddles/django-coalesce
Generate boilerplate API code from your Django models
https://github.com/joeriddles/django-coalesce
django python
Last synced: 2 months ago
JSON representation
Generate boilerplate API code from your Django models
- Host: GitHub
- URL: https://github.com/joeriddles/django-coalesce
- Owner: joeriddles
- License: apache-2.0
- Created: 2024-01-24T07:30:33.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-08T05:18:59.000Z (about 2 years ago)
- Last Synced: 2025-03-26T02:45:44.706Z (over 1 year ago)
- Topics: django, python
- Language: Python
- Homepage: https://pypi.org/project/django-coalesce/
- Size: 45.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-coalesce
[](https://badge.fury.io/py/django-coalesce)
What makes the world's most powerful batteries-included web framework even better? Automatically generating more batteries! 🔋
Inspired by the awesome .NET [Coalesce](https://intellitect.github.io/Coalesce/) project for accelerated web app development.
## Features
### Generate [Django Ninja](https://django-ninja.dev/) API CRUD views — WIP
Given the following Django model:
```python
# models.py
class User(models.Model):
id = models.BigAutoField(primary_key=True)
email = models.CharField()
```
Generates the following Django Ninja module:
```python
# user_api.g.py
from django.shortcuts import get_object_or_404
from ninja import Router, Schema
from blog.models import User
router = Router()
class UserIn(Schema):
email: str
class UserOut(Schema):
id: int
email: str
@router.post("/", response=UserOut)
def create_user(request, payload: UserIn):
user = User.objects.create(**payload.dict())
return {"id": user.id}
@router.get("/{user_id}/", response=UserOut)
def get_user(request, user_id: int):
user = get_object_or_404(User, id=user_id)
return user
@router.get("/", response=list[UserOut])
def list_users(request):
users = User.objects.all()
return users
@router.put("/{user_id}/")
def update_user(request, user_id: int, payload: UserIn):
user = get_object_or_404(User, id=user_id)
for attr, value in payload.dict().items():
setattr(user, attr, value)
user.save()
return {"success": True}
@router.delete("/{user_id}/")
def delete_user(request, user_id: int):
user = get_object_or_404(User, id=user_id)
user.delete()
return {"success": True}
```
### Generate TypeScript models from Django models — WIP
Given the following Django model:
```python
# models.py
class User(models.Model):
id = models.BigAutoField(primary_key=True)
email = models.CharField()
```
Generates the following TypeScript model:
```typescript
// user.g.ts
export interface user {
id: number;
email: string;
}
```
## Development
### Getting Started
```shell
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -e '.[dev]'
# not required but makes setting PYTHONPATH and DJANGO_SETTINGS_MODULE easier
direnv allow
```
### Testing
Run all tests
```shell
cd ./src/django_coalesce
pytest .
```
Run tests with coverage
```shell
cd ./src/django_coalesce
pytest --cov=django_coalesce .
```
### Build
```shell
python3 -m pip install --upgrade build twine
python3 -m build
```
Upload to [test.pypi.org](https://test.pypi.org)
```shell
python3 -m twine upload --repository testpypi dist/*
```
Upload to [PyPI](https://pypi.org)
```shell
python3 -m twine upload dist/*
```