Skip to main content

A Django-like but async web-framework based on FastAPI and TortoiseORM.

Project description

OhMyAPI

OhMyAPI == Application scaffolding for FastAPI+TortoiseORM.

OhMyAPI is a blazingly fast, async Python web application framework with batteries included. It is built around FastAPI and TortoiseORM and is thus 100% async.

Features:

  • Django-like project-layout and -structure
  • Django-like settings.py
  • Django-like models via TortoiseORM
  • Django-like model.Meta class for model configuration
  • Django-like advanced permissions system
  • Django-like migrations (makemigrations & migrate) via Aerich
  • Django-like CLI for interfacing with your projects (startproject, startapp, shell, serve, etc)
  • various optional builtin apps
  • highly configurable and customizable
  • 100% async

Getting started

Creating a Project

pip install ohmyapi  # TODO: not yet published
ohmyapi startproject myproject
cd myproject

This will create the following directory structure:

myproject/
  - pyproject.toml
  - settings.py

Run your project with:

ohmyapi serve

In your browser go to:

Creating an App

Create a new app by:

ohmyapi startapp myapp

This will lead to the following directory structure:

myproject/
  - myapp/
    - __init__.py
    - models.py
    - routes.py
  - pyproject.toml
  - settings.py

Add 'myapp' to your INSTALLED_APPS in settings.py.

Write your first model in myapp/models.py:

from ohmyapi.db import Model, field


class Person(Model):
    id: int = field.IntField(min=1, pk=True)
    name: str = field.CharField(min_length=1, max_length=255)
    username: str = field.CharField(min_length=1, max_length=255, unique=True)
    age: int = field.IntField(min=0)

Next, create your endpoints in myapp/routes.py:

from fastapi import APIRouter, HTTPException
from tortoise.exceptions import DoesNotExist

from .models import Person

router = APIRouter(prefix="/myapp")


@router.get("/")
async def list():
    return await Person.all()


@router.get("/:id")
async def get(id: int):
    try:
        await Person.get(pk=id)
    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/
  - myapp/
    - __init__.py
    - models.py
    - routes.py
  - migrations/
    - myapp/
  - pyproject.toml
  - 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 auth tables to be created in the database.

settings.py:

INSTALLED_APPS = [
    'ohmyapi_auth',
    ...
]

JWT_SECRET = "t0ps3cr3t"

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

ohmyapi-0.1.1.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

ohmyapi-0.1.1-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ohmyapi-0.1.1.tar.gz
  • Upload date:
  • Size: 11.2 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

Hashes for ohmyapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 32bee5d4c5e4f0c5edb36bb74503b25d6806ddb7fb6aa4c82f885ea8e2b759e3
MD5 9565759e2c7c7c1f137588a7821d5088
BLAKE2b-256 161d29fcec719f9b0c24b13bcbfb3d3b619a9fb84191d26ebbba3c4538b9120b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ohmyapi-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.2 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

Hashes for ohmyapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 325c9f5765fbf0bb70dce0535f2d6e400c75dd667842b8071ccaf072b89d2b9b
MD5 60093d2c95f1f6433dff8cd7fa7b2672
BLAKE2b-256 36dbc4a9cff80d149311fb823cd8b1bf2cd2dc4b94712ab7e778bc941419c5b0

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