A Django-like but async web-framework based on FastAPI and TortoiseORM.
Project description
OhMyAPI
Think: Micro-Django, but API-first, less clunky and 100% async.
OhMyAPI is a Django-flavored web-application scaffolding framework and management layer. Built around FastAPI and TortoiseORM, it is 100% async.
It is blazingly fast, fun to use and comes with batteries included!
Features
- Django-like project-layout and -structure
- Django-like prject-level settings.py
- Django-like models via TortoiseORM
- Django-like
Model.Metaclass for model configuration - Easily convert your query results to
pydanticmodels viaModel.Schema - Django-like migrations (makemigrations & migrate) via Aerich
- Django-like CLI tooling (
startproject,startapp,shell,serve, etc) - Various optional builtin apps you can hook into your project
- Highly configurable and customizable
- 100% async
Getting started
Creating a Project
pip install ohmyapi
ohmyapi startproject myproject
cd myproject
This will create the following directory structure:
myproject/
- pyproject.toml
- README.md
- settings.py
Run your project with:
ohmyapi serve
In your browser go to:
Creating an App
Create a new app by:
ohmyapi startapp tournament
This will create the following directory structure:
myproject/
- tournament/
- __init__.py
- models.py
- routes.py
- pyproject.toml
- README.md
- settings.py
Add 'tournament' to your INSTALLED_APPS in settings.py.
Models
Write your first model in turnament/models.py:
from ohmyapi.db import Model, field
class Tournament(Model):
id = field.IntField(primary_key=True)
name = field.TextField()
created = field.DatetimeField(auto_now_add=True)
def __str__(self):
return self.name
class Event(Model):
id = field.IntField(primary_key=True)
name = field.TextField()
tournament = field.ForeignKeyField('tournament.Tournament', related_name='events')
participants = field.ManyToManyField('torunament.Team', related_name='events', through='event_team')
modified = field.DatetimeField(auto_now=True)
prize = field.DecimalField(max_digits=10, decimal_places=2, null=True)
def __str__(self):
return self.name
class Team(Model):
id = field.IntField(primary_key=True)
name = field.TextField()
def __str__(self):
return self.name
API Routes
Next, create your endpoints in tournament/routes.py:
from ohmyapi.router import APIRouter, HTTPException
from ohmyapi.db.exceptions import DoesNotExist
from .models import Tournament
router = APIRouter(prefix="/tournament")
@router.get("/")
async def list():
queryset = Tournament.all()
return await Tournament.Schema.many.from_queryset(queryset)
@router.get("/:id")
async def get(id: int):
try:
queryset = Tournament.get(pk=id)
return await Tournament.Schema.one(queryset)
except DoesNotExist:
raise HTTPException(status_code=404, detail="item not found")
...
Migrations
Before we can run the app, we need to create and initialize the database.
Similar to Django, first run:
ohmyapi makemigrations [ <app> ] # no app means all INSTALLED_APPS
This will create a migrations/ folder in you project root.
myproject/
- tournament/
- __init__.py
- models.py
- routes.py
- migrations/
- tournament/
- pyproject.toml
- README.md
- settings.py
Apply your migrations via:
ohmyapi migrate [ <app> ] # no app means all INSTALLED_APPS
Run your project:
ohmyapi serve
Shell
Similar to Django, you can attach to an interactive shell with your project already loaded inside.
ohmyapi shell
Authentication
A builtin auth app is available.
Simply add ohmyapi_auth to your INSTALLED_APPS and define a JWT_SECRET in your settings.py.
Remember to makemigrations and migrate for the necessary tables to be created in the database.
settings.py:
INSTALLED_APPS = [
'ohmyapi_auth',
...
]
JWT_SECRET = "t0ps3cr3t"
After restarting your project you will have access to the ohmyapi_auth app.
It comes with a User and Group model, as well as endpoints for JWT auth.
You can use the models as ForeignKeyField in your application models:
class Team(Model):
[...]
members = field.ManyToManyField('ohmyapi_auth.User', related_name='tournament_teams', through='tournament_teams')
[...]
Remember to run makemigrations and migrate in order for your model changes to take effect in the database.
Create a super-user:
ohmyapi createsuperuser
Permissions
API-Level Permissions
Use FastAPI's Depends pattern to implement API-level access-control.
In your routes.py:
from ohmyapi.router import APIRouter, Depends
from ohmyapi_auth.models import User
from ohmyapi_auth import (
models as auth,
permissions,
)
from .models import Tournament
router = APIRouter(prefix="/tournament")
@router.get("/")
async def list(user: auth.User = Depends(permissions.require_authenticated)):
queryset = Tournament.all()
return await Tournament.Schema.many.from_queryset(queryset)
...
Model-Level Permissions
Use Tortoise's Manager to implement model-layer permissions.
from ohmyapi.db import Manager
from typing import Callable
class TeamManager(Manager):
async def for_user(self, user):
return await self.filter(members=user).all()
class Team(Model):
[...]
class Meta:
manager = TeamManager()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ohmyapi-0.1.7.tar.gz.
File metadata
- Download URL: ohmyapi-0.1.7.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.0 CPython/3.13.7 Linux/6.16.8-zen3-1-zen
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b900132aac617213a866ffafbd3a4660a9acecdbfae672932b83e72a35a61cc
|
|
| MD5 |
3268ee1f0eeb5b45c94a883c34b8c5c5
|
|
| BLAKE2b-256 |
b7da662f934fdea54cc7a6a404d14a8a44602d2335e3a57d250093f1b9053cb4
|
File details
Details for the file ohmyapi-0.1.7-py3-none-any.whl.
File metadata
- Download URL: ohmyapi-0.1.7-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.0 CPython/3.13.7 Linux/6.16.8-zen3-1-zen
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c8851851cf779491064b4a86ad612e0813a065d0c06e33b98b15417890fdb8d
|
|
| MD5 |
71b5d0c369dc1d38487038df3054bcf2
|
|
| BLAKE2b-256 |
4de52846f677a3c1e968adb6860e5a2cee717d62fe19eac09bd4e786b473ad2d
|